Python:脚本的目录 [英] Python: script's directory

查看:87
本文介绍了Python:脚本的目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个解决方案,但没有找到我需要的。

I was looking for a solution, but have not found what I need.

脚本路径: /dir/to/script/script.py C:\dir\script.py

例外结果:

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

是否任何功能在os模块或东西?

Is there any function in os module or something?

我混合解决方案并写:

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

但它是丑陋的有更好的方法吗?

But it's ugly. Is there better way?

推荐答案

os.path.realpath会给你结果:

os.path.realpath will give you the result:

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

这篇关于Python:脚本的目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆