查找当前运行文件的路径

发布于 2024-08-02 09:52:58 字数 139 浏览 6 评论 0原文

如何找到当前运行的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

や莫失莫忘 2024-08-09 09:52:58

__file__ 不是您要寻找的内容。不要使用意外的副作用

sys.argv[0]始终< /strong> 脚本的路径(如果实际上已调用脚本)--参见 http://docs.python.org/library/sys.html#sys.argv

__file__当前正在执行文件的路径(脚本或模块)。如果从脚本访问的话,这意外与脚本相同!如果您想将有用的内容(例如相对于脚本位置定位资源文件)放入库中,则必须使用 sys.argv[0]。

例子:

C:\junk\so>type \junk\so\scriptpath\script1.py
import sys, os
print "script: sys.argv[0] is", repr(sys.argv[0])
print "script: __file__ is", repr(__file__)
print "script: cwd is", repr(os.getcwd())
import whereutils
whereutils.show_where()

C:\junk\so>type \python26\lib\site-packages\whereutils.py
import sys, os
def show_where():
    print "show_where: sys.argv[0] is", repr(sys.argv[0])
    print "show_where: __file__ is", repr(__file__)
    print "show_where: cwd is", repr(os.getcwd())

C:\junk\so>\python26\python scriptpath\script1.py
script: sys.argv[0] is 'scriptpath\\script1.py'
script: __file__ is 'scriptpath\\script1.py'
script: cwd is 'C:\\junk\\so'
show_where: sys.argv[0] is 'scriptpath\\script1.py'
show_where: __file__ is 'C:\\python26\\lib\\site-packages\\whereutils.pyc'
show_where: cwd is 'C:\\junk\\so'

__file__ is NOT what you are looking for. Don't use accidental side-effects

sys.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 use sys.argv[0].

Example:

C:\junk\so>type \junk\so\scriptpath\script1.py
import sys, os
print "script: sys.argv[0] is", repr(sys.argv[0])
print "script: __file__ is", repr(__file__)
print "script: cwd is", repr(os.getcwd())
import whereutils
whereutils.show_where()

C:\junk\so>type \python26\lib\site-packages\whereutils.py
import sys, os
def show_where():
    print "show_where: sys.argv[0] is", repr(sys.argv[0])
    print "show_where: __file__ is", repr(__file__)
    print "show_where: cwd is", repr(os.getcwd())

C:\junk\so>\python26\python scriptpath\script1.py
script: sys.argv[0] is 'scriptpath\\script1.py'
script: __file__ is 'scriptpath\\script1.py'
script: cwd is 'C:\\junk\\so'
show_where: sys.argv[0] is 'scriptpath\\script1.py'
show_where: __file__ is 'C:\\python26\\lib\\site-packages\\whereutils.pyc'
show_where: cwd is 'C:\\junk\\so'
滴情不沾 2024-08-09 09:52:58

这将打印脚本所在的目录(而不是工作目录):

import os
dirname, filename = os.path.split(os.path.abspath(__file__))
print "running from", dirname
print "file is", filename

以下是当我将其放入 c:\src 时它的行为方式:

> cd c:\src
> python so-where.py
running from C:\src
file is so-where.py

> cd c:\
> python src\so-where.py
running from C:\src
file is so-where.py

This will print the directory in which the script lives (as opposed to the working directory):

import os
dirname, filename = os.path.split(os.path.abspath(__file__))
print "running from", dirname
print "file is", filename

Here's how it behaves, when I put it in c:\src:

> cd c:\src
> python so-where.py
running from C:\src
file is so-where.py

> cd c:\
> python src\so-where.py
running from C:\src
file is so-where.py
穿透光 2024-08-09 09:52:58
import sys, os

file = sys.argv[0]
pathname = os.path.dirname(file)
print 'running from %s' % os.path.abspath(pathname)
print 'file is %s' % file

检查 os.getcwd() (文档

import sys, os

file = sys.argv[0]
pathname = os.path.dirname(file)
print 'running from %s' % os.path.abspath(pathname)
print 'file is %s' % file

Check os.getcwd() (docs)

醉南桥 2024-08-09 09:52:58

运行文件始终是__file__

这是一个演示脚本,名为 identify.py

print __file__

这是结果

MacBook-5:Projects slott$ python StackOverflow/identify.py 
StackOverflow/identify.py
MacBook-5:Projects slott$ cd StackOverflow/
MacBook-5:StackOverflow slott$ python identify.py 
identify.py

The running file is always __file__.

Here's a demo script, named identify.py

print __file__

Here's the results

MacBook-5:Projects slott$ python StackOverflow/identify.py 
StackOverflow/identify.py
MacBook-5:Projects slott$ cd StackOverflow/
MacBook-5:StackOverflow slott$ python identify.py 
identify.py
毅然前行 2024-08-09 09:52:58

我建议

import os, sys
print os.path.split(os.path.abspath(os.path.realpath(sys.argv[0])))[0]

您通过这种方式可以安全地创建到脚本可执行文件的符号链接,并且它仍然会找到正确的目录。

I would suggest

import os, sys
print os.path.split(os.path.abspath(os.path.realpath(sys.argv[0])))[0]

This way you can safely create symbolic links to the script executable and it will still find the correct directory.

狂之美人 2024-08-09 09:52:58

脚本名称将(总是?)是 sys.argv 的第一个索引:

import sys
print sys.argv[0]

查找正在运行的脚本的路径的更简单方法:

os.path.dirname(sys.argv[0])

The script name will (always?) be the first index of sys.argv:

import sys
print sys.argv[0]

An even easier way to find the path of your running script:

os.path.dirname(sys.argv[0])
岁月苍老的讽刺 2024-08-09 09:52:58

python正在执行的脚本的目录被添加到sys.path
这实际上是一个包含其他路径的数组(列表)。
第一个元素包含脚本所在的完整路径(对于 Windows)。

因此,对于 Windows,可以使用:

import sys
path = sys.path[0]
print(path)

其他人建议使用 sys.argv[0] ,它的工作方式非常相似并且完整。

import sys
path = os.path.dirname(sys.argv[0])
print(path)

请注意,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:

import sys
path = sys.path[0]
print(path)

Others have suggested using sys.argv[0] which works in a very similar way and is complete.

import sys
path = os.path.dirname(sys.argv[0])
print(path)

Note that sys.argv[0] contains the full working directory (path) + filename whereas sys.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.

对岸观火 2024-08-09 09:52:58

除了前面提到的 sys.argv[0] 之外,还可以使用 __main__

import __main__
print(__main__.__file__)

但是要注意,这仅在极少数情况下有用;
并且它总是会创建一个导入循环,这意味着 __main__ 在那一刻不会完全执行。

Aside from the aforementioned sys.argv[0], it is also possible to use the __main__:

import __main__
print(__main__.__file__)

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文