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.
- In a.py, how to detect /X/Y/? I mean, how to know in which directory the python call is made?
You can get the current working directory with:
os.getcwd()
>> 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
精彩评论