如何获得正在运行的Python脚本的路径?(复制)
16lz
2021-01-22
Duplicate:
In Python, how do I get the path and name of the file that is currently executing?Duplicate:在Python中,如何获取当前正在执行的文件的路径和名称?
How do I get the path of a the Python script I am running in? I was doing dirname(sys.argv[0])
, however on Mac I only get the filename - not the full path as I do on Windows.
如何获得正在运行的Python脚本的路径?我正在做dirname(sys.argv[0]),但是在Mac上,我只得到文件名,而不是像我在Windows上那样的完整路径。
No matter where my application is launched from, I want to open files that are relative to my script file(s).
无论我的应用程序是从哪里启动的,我都希望打开与我的脚本文件相关的文件。
5 个解决方案
#1
342
os.path.realpath(__file__)
will give you the path of the current file, resolving any symlinks in the path. This works fine on my mac.
realpath(__file__)将为您提供当前文件的路径,解析路径中的任何符号链接。这在我的mac上很好用。
更多相关文章
- python 处理csv文件的过程对换行符的处理
- linux修改文件所属用户和组
- Linux的文件权限
- linux系统更改目录和文件的权限总结
- CentOS7.2 通过nfs设置共享文件夹
- linux下查找包含关键字的文件
- Linux系统下Tar文件安装方法
- Linux学习笔记(九)--RedHat 7.0之用户身份与文件权限、存储结构与
- 在Linux用tar归档压缩文件时忽略某些文件和目录