Back

Explore Courses Blog Tutorials Interview Questions
0 votes
3 views
in Python by (16.4k points)
I want to know, What are all the commands I have to use to find the current directory and where actually the file I am executing is? I want to know the commands of these two questions?

1 Answer

0 votes
by (26.4k points)

Command to get the current working directory use:

import os

cwd = os.getcwd()

Command which will get the entire path to the directory where a python file is contained in:

import os 

dir_path = os.path.dirname(os.path.realpath(__file__))

Make sure that the spell above won't work on the off chance that you've just utilized os.chdir() to change your present working registry, since the estimation of the __file__ constant is comparative with the current working directory and isn't changed by an os.chdir() call.

Wanna become a python expert? Come and join the python certification course & get certified.

Related questions

0 votes
1 answer
asked Sep 16, 2019 in Python by Sammy (47.6k points)
+1 vote
2 answers
0 votes
1 answer
+1 vote
1 answer
0 votes
1 answer

Browse Categories

...