Cjh1552
Cjh1552

Reputation: 25

How do i get the path of the python script i am running in for multiple people

im trying to find out what the path of the file would be to delete it

os.remove(PATHTOFILE) 

but im not sure how to get the exact path of the file, the file would be in the same directory as the script but as this script will be on different users in the future im not sure how i would be able to detect that an change the path

Upvotes: 0

Views: 69

Answers (2)

Leo K
Leo K

Reputation: 5354

Use sys.argv[0]. You should read it and convert it to an absolute path with os.path.abspath(sys.argv[0]). Do this early, before any code in your Python script calls os.chdir().

Then, you can get the directory part of the script's file name.

Upvotes: 1

Shakes
Shakes

Reputation: 521

import inspect, os
print inspect.getfile(inspect.currentframe()) # file name and path of script
print os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))) # directory script is running in

See this answered question

Upvotes: 1

Related Questions