Python: script's directory

白昼怎懂夜的黑 提交于 2019-12-21 04:33:10

问题


I was looking for a solution, but have not found what I need.

Script path: /dir/to/script/script.py or C:\dir\script.py

Excepted result:

$ ./script.py
output: /dir/to/script
$ cd .. && ./script/script.py
output: /dir/to/script

Is there any function in os module or something?


I mixed solutions and write:

print os.path.abspath(os.path.dirname(__file__))

But it's ugly. Is there better way?


回答1:


os.path.realpath will give you the result:

os.path.dirname(os.path.realpath(__file__))


来源:https://stackoverflow.com/questions/7293601/python-scripts-directory

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!