简体   繁体   English

Python:脚本的目录

[英]Python: script's directory

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 脚本路径: /dir/to/script/script.pyC:\\ 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? os模块中有什么功能吗?


I mixed solutions and write: 我混合解决方案写道:

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

But it's ugly. 但它很难看。 Is there better way? 还有更好的方法吗?

os.path.realpath会给你结果:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM