开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜