Python:脚本的目录

标签 python directory

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

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

异常结果:

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

os模块有什么功能吗?


我混合解决方案并写下:

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

但是很丑。有更好的方法吗?

最佳答案

os.path.realpath 会给你结果:

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

关于Python:脚本的目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7293601/

相关文章:

xml - XSL,获取当前工作目录

python - 当父记录存在时,如何在 django rest 框架中的子序列化程序中设置外键值

python - 在 For 循环中获取属性名称

python - 从索引开始遍历 python 字符串

处理 Zip 文件的 java 库

Linux 文件类型

bash - 使用 Bash 删除除某些文件和目录之外的所有文件和目录

C OS X 将目录内容读入字符串数组

python - 在python中更新字典的整数值

python - 属性错误 : module 'matplotlib.pyplot' has no attribute 'xlable'