Python:脚本目录

2024-04-18 20:42:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我在寻找解决办法,但还没有找到我需要的。

脚本路径:/dir/to/Script/Script.pyC:\ dir\Script.py

例外结果:

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

操作系统模块里有什么功能吗?


我混合解决方案并写下:

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

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


Tags: 模块topathpy路径功能脚本output