prosseek
prosseek

Reputation: 190679

How to know the directory where the python script is called?

Let's say that I have a python script a.py in /A/B/a.py, and it's in the PATH environment variable. The current working directory is /X/Y/, and it's the directory where I call the /A/B/a.py.

Upvotes: 27

Views: 16893

Answers (2)

Radomir Dopieralski
Radomir Dopieralski

Reputation: 2585

You can get the current working directory with:

os.getcwd()

Upvotes: 37

Matthew Rankin
Matthew Rankin

Reputation: 461157

>> os.getcwd()
/X/Y
>> os.path.dirname(os.path.realpath(__file__))   # cannot be called interactively
/A/B
>> sys.path[0]
/A/B
>> os.path.abspath(sys.argv[0])
/A/B/a.py

Upvotes: 8

Related Questions