Danny
Danny

Reputation: 5400

Reference where a python script is located if being run from another location

I'm trying to run a python script from a different location than where it is located but I need to reference files where the python script is located. I tried the following:

os.chdir(os.getcwd())

But unfortunately that gets the current working dir of where I ran the script from, not where the script currently lives.

Thanks

Upvotes: 3

Views: 780

Answers (1)

Sven Marnach
Sven Marnach

Reputation: 601679

You can use

os.path.dirname(__file__)

to get the directory your script is located in.

Upvotes: 5

Related Questions