I am trying to get the path of my python script.
I know ‘sys.argv[0] gives me the path and the name of my python script.
how can I just get the path?
I tried:
print sys.argv[0]
path = sys.argv[0].split("/")
scriptpath = "".join(path[0:-1])
But it does not add back the path separator.
Prefer to use
__file__, like this:Note: using
sys.argv[0]may not work if you call the script via another script from another directory.