Reputation: 25
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
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
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
Upvotes: 1