查找当前运行文件的路径
如何找到当前运行的 Python 脚本的完整路径?也就是说,我必须做什么才能实现这一目标:
$ pwd
/tmp
$ python baz.py
running from /tmp
file is baz.py
How can I find the full path to the currently running Python script? That is to say, what do I have to do to achieve this:
$ pwd
/tmp
$ python baz.py
running from /tmp
file is baz.py
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
__file__
不是您要寻找的内容。不要使用意外的副作用sys.argv[0]
是始终< /strong> 脚本的路径(如果实际上已调用脚本)--参见 http://docs.python.org/library/sys.html#sys.argv__file__
是当前正在执行文件的路径(脚本或模块)。如果从脚本访问的话,这意外与脚本相同!如果您想将有用的内容(例如相对于脚本位置定位资源文件)放入库中,则必须使用 sys.argv[0]。例子:
__file__
is NOT what you are looking for. Don't use accidental side-effectssys.argv[0]
is always the path to the script (if in fact a script has been invoked) -- see http://docs.python.org/library/sys.html#sys.argv__file__
is the path of the currently executing file (script or module). This is accidentally the same as the script if it is accessed from the script! If you want to put useful things like locating resource files relative to the script location into a library, then you must usesys.argv[0]
.Example:
这将打印脚本所在的目录(而不是工作目录):
以下是当我将其放入
c:\src
时它的行为方式:This will print the directory in which the script lives (as opposed to the working directory):
Here's how it behaves, when I put it in
c:\src
:检查 os.getcwd() (文档)
Check os.getcwd() (docs)
运行文件始终是
__file__
。这是一个演示脚本,名为
identify.py
这是结果
The running file is always
__file__
.Here's a demo script, named
identify.py
Here's the results
我建议
您通过这种方式可以安全地创建到脚本可执行文件的符号链接,并且它仍然会找到正确的目录。
I would suggest
This way you can safely create symbolic links to the script executable and it will still find the correct directory.
脚本名称将(总是?)是 sys.argv 的第一个索引:
查找正在运行的脚本的路径的更简单方法:
The script name will (always?) be the first index of sys.argv:
An even easier way to find the path of your running script:
python正在执行的脚本的目录被添加到
sys.path
这实际上是一个包含其他路径的数组(列表)。
第一个元素包含脚本所在的完整路径(对于 Windows)。
因此,对于 Windows,可以使用:
其他人建议使用 sys.argv[0] ,它的工作方式非常相似并且完整。
请注意,
sys.argv[0]
包含完整的工作目录(路径)+ 文件名,而sys.path[0]
是不带文件名的当前工作目录。我已经在 Windows 上测试了
sys.path[0]
并且它有效。我还没有在 Windows 之外的其他操作系统上进行过测试,所以有人可能希望对此发表评论。The directory of the script which python is executing is added to
sys.path
This is actually an array (list) which contains other paths.
The first element contains the full path where the script is located (for windows).
Therefore, for windows, one can use:
Others have suggested using
sys.argv[0]
which works in a very similar way and is complete.Note that
sys.argv[0]
contains the full working directory (path) + filename whereassys.path[0]
is the current working directory without the filename.I have tested
sys.path[0]
on windows and it works. I have not tested on other operating systems outside of windows, so somebody may wish to comment on this.除了前面提到的
sys.argv[0]
之外,还可以使用__main__
:但是要注意,这仅在极少数情况下有用;
并且它总是会创建一个导入循环,这意味着 __main__ 在那一刻不会完全执行。
Aside from the aforementioned
sys.argv[0]
, it is also possible to use the__main__
:Beware, however, this is only useful in very rare circumstances;
and it always creates an import loop, meaning that the
__main__
will not be fully executed at that moment.