开发者

Python: script's directory

开发者 https://www.devze.com 2023-04-02 00:13 出处:网络
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

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 f开发者_如何学Gounction 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?


os.path.realpath will give you the result:

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

精彩评论

暂无评论...
验证码 换一张
取 消