如何在Python中获取当前执行文件的路径?

这看起来像一个新手问题,但事实并非如此。 一些常用方法在所有情况下都不起作用:

sys.argv中[0]

这意味着使用path = os.path.abspath(os.path.dirname(sys.argv[0])) ,但如果您从另一个目录中的另一个Python脚本运行,则这不起作用,并且这可能会在现实生活中发生。

__文件__

这意味着使用path = os.path.abspath(os.path.dirname(__file__)) ,但我发现这不起作用:

  • py2exe没有__file__属性,但有一个解决方法
  • 当你使用execute()从IDLE运行时,没有__file__属性
  • OS X 10.6我得到的NameError: global name '__file__' is not defined
  • 有不完整答案的相关问题:

  • Python - 查找正在运行的文件的路径
  • 当前文件的路径取决于我如何执行程序
  • 如何知道在Python中运行脚本的路径?
  • 将目录切换到Python脚本的目录
  • 我正在寻找一种通用的解决方案 ,可以在所有上述用例中使用。

    更新

    这是一个测试用例的结果:

    python a.py的输出(在Windows上)

    a.py: __file__= a.py
    a.py: os.getcwd()= C:zzz
    
    b.py: sys.argv[0]= a.py
    b.py: __file__= a.py
    b.py: os.getcwd()= C:zzz
    

    a.py

    #! /usr/bin/env python
    import os, sys
    
    print "a.py: sys.argv[0]=", sys.argv[0]
    print "a.py: __file__=", __file__
    print "a.py: os.getcwd()=", os.getcwd()
    print
    
    execfile("subdir/b.py")
    

    子目录/ b.py

    #! /usr/bin/env python
    import os, sys
    
    print "b.py: sys.argv[0]=", sys.argv[0]
    print "b.py: __file__=", __file__
    print "b.py: os.getcwd()=", os.getcwd()
    print
    

    C:.
    |   a.py
    ---subdir
            b.py
    

    您不能直接确定正在执行的主脚本的位置。 毕竟,有时脚本根本不来自文件。 例如,它可能来自交互式解释器或仅存储在内存中的动态生成的代码。

    但是,您可以可靠地确定模块的位置,因为模块始终是从文件加载的。 如果使用以下代码创建模块并将其放在与主脚本相同的目录中,那么主脚本可以导入模块并使用它来定位它自己。

    SOME_PATH / module_locator.py:

    def we_are_frozen():
        # All of the modules are built-in to the interpreter, e.g., by py2exe
        return hasattr(sys, "frozen")
    
    def module_path():
        encoding = sys.getfilesystemencoding()
        if we_are_frozen():
            return os.path.dirname(unicode(sys.executable, encoding))
        return os.path.dirname(unicode(__file__, encoding))
    

    SOME_PATH / main.py:

    import module_locator
    my_path = module_locator.module_path()
    

    如果在不同目录中有几个主要脚本,则可能需要module_locator的多个副本。

    当然,如果您的主脚本是由其他工具加载的,而这些工具不允许导入与脚本位于同一位置的模块,那么您的运气不好。 在这种情况下,你所追踪的信息根本就不存在于程序中的任何地方。 你最好的选择是向工具的作者提交一个错误。


    首先,你需要从inspectos导入

    from inspect import getsourcefile
    from os.path import abspath
    

    接下来,无论你想从你那里找到源文件,只要使用

    abspath(getsourcefile(lambda:0))
    

    我遇到了类似的问题,我认为这可能会解决问题:

    def module_path(local_function):
       ''' returns the module path without the use of __file__.  Requires a function defined
       locally in the module.
       from http://stackoverflow.com/questions/729583/getting-file-path-of-imported-module'''
       return os.path.abspath(inspect.getsourcefile(local_function))
    

    它适用于普通脚本和空闲。 我所能说的只是为别人尝试!

    我的典型用法:

    from toolbox import module_path
    def main():
       pass # Do stuff
    
    global __modpath__
    __modpath__ = module_path(main)
    

    现在我使用__modpath__而不是__file__。

    链接地址: http://www.djcxy.com/p/9285.html

    上一篇: How do I get the path of the current executed file in Python?

    下一篇: How to know/change current directory in Python shell?