Python:脚本的目录

2024-06-28

我一直在寻找解决方案,但尚未找到我需要的。

脚本路径:/dir/to/script/script.py or 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__))

但它很丑。有更好的办法吗?


os.path.realpath 会给你结果:

os.path.dirname(os.path.realpath(__file__))
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Python:脚本的目录 的相关文章

随机推荐