开发者

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜