Python:脚本的目录

时间:2011-09-03 14:08:55

标签: python directory

我一直在寻找解决方案,但还没找到我需要的东西。

脚本路径: /dir/to/script/script.py C:\ dir \ script.py

例外结果:

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

os模块中是否有任何功能?


我混合了解决方案并写道:

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

但它很难看。还有更好的办法吗?

1 个答案:

答案 0 :(得分:15)

os.path.realpath将为您提供结果:

os.path.dirname(os.path.realpath(__file__))
相关问题