如何获取正在运行的Python脚本的路径? [英] How do I get the path of the Python script I am running in?
问题描述
Duplicate:
In Python, how do I get the path and name of the file that is currently executing?
如何获取正在运行的Python脚本的路径?我正在做dirname(sys.argv[0])
,但是在Mac上我只获取文件名-而不是Windows上的完整路径.
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.
无论从哪里启动应用程序,我都想打开与脚本文件相关的文件.
No matter where my application is launched from, I want to open files that are relative to my script file(s).
推荐答案
os.path.realpath(__file__)
将为您提供当前文件的路径,从而解决该路径中的任何符号链接.在我的Mac上可以正常工作.
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.
这篇关于如何获取正在运行的Python脚本的路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!