首页 文章

如何使用Python查找脚本的目录? [重复]

提问于
浏览
369

这个问题在这里已有答案:

请考虑以下Python代码:

import os
print os.getcwd()

我使用 os.getcwd()get the script file's directory location . 当我从命令行运行脚本时它给了我正确的路径,而当我从Django视图中的代码运行的脚本运行它时,它打印 / .

如何从Django视图运行的脚本中获取脚本的路径?

UPDATE:
总结到目前为止的答案 - os.getcwd()os.path.abspath() 都给出了当前的工作目录,该目录可能是也可能不是脚本所在的目录 . 在我的Web主机设置中, __file__ 仅提供没有路径的文件名 .

Python中是否有任何方法(总是)能够接收脚本所在的路径?

12 回答

  • 5

    您需要在 __file__ 上调用 os.path.realpath ,这样当 __file__ 是没有路径的文件名时,您仍然可以获得目录路径:

    import os
    print(os.path.dirname(os.path.realpath(__file__)))
    
  • 15

    试试 sys.path[0] .

    引用Python文档:

    在程序启动时初始化时,此列表的第一项path [0]是包含用于调用Python解释器的脚本的目录 . 如果脚本目录不可用(例如,如果以交互方式调用解释器或者从标准输入读取脚本),则路径[0]为空字符串,它指示Python首先搜索当前目录中的模块 . 请注意,在作为PYTHONPATH的结果插入条目之前插入了脚本目录 .

    资料来源:https://docs.python.org/library/sys.html#sys.path

  • 13

    我用:

    import os
    import sys
    
    def get_script_path():
        return os.path.dirname(os.path.realpath(sys.argv[0]))
    

    正如aiham在评论中指出的那样,您可以在模块中定义此函数并在不同的脚本中使用它 .

  • 0

    这段代码:

    import os
    dn = os.path.dirname(os.path.realpath(__file__))
    

    将“dn”设置为包含当前正在执行的脚本的目录的名称 . 这段代码:

    fn = os.path.join(dn,"vcb.init")
    fp = open(fn,"r")
    

    将“fn”设置为“script_dir / vcb.init”(以独立于平台的方式)并打开该文件以供当前正在执行的脚本读取 .

    请注意"the currently executing script"有点含糊不清 . 如果您的整个程序包含1个脚本,那么这是当前正在执行的脚本,并且"sys.path[0]"解决方案正常工作 . 但是,如果您的应用程序包含脚本A,它导入一些包"P"然后调用脚本"B",则"P.B"当前正在执行 . 如果需要获取包含"P.B"的目录,则需要“ os.path.realpath(__file__) ”解决方案 .

    __file__ " just gives the name of the currently executing (top-of-stack) script: " x.py ". It doesn't give any path info. It's the " os.path.realpath”调用可以完成真正的工作 .

  • 1
    import os,sys
    # Store current working directory
    pwd = os.path.dirname(__file__)
    # Append current directory to the python path
    sys.path.append(pwd)
    
  • -1
    import os
    script_dir = os.path.dirname(os.path.realpath(__file__)) + os.sep
    
  • 593

    这对我有用(我通过this stackoverflow question找到了它)

    os.path.realpath(__file__)
    
  • 2

    这就是我最终的结果 . 如果我在解释器中导入脚本,并且如果我将其作为脚本执行,这对我有用:

    import os
    import sys
    
    # Returns the directory the current script (or interpreter) is running in
    def get_script_directory():
        path = os.path.realpath(sys.argv[0])
        if os.path.isdir(path):
            return path
        else:
            return os.path.dirname(path)
    
  • 2

    这是一个非常古老的线程,但是当我尝试将文件保存到脚本所在的当前目录中时,我遇到了这个问题,从cron作业运行python脚本 . getcwd()和很多其他路径出现在您的主目录中 .

    获得我使用的脚本的绝对路径

    directory = os.path.abspath(os.path.dirname(__file__))

  • 122

    使用 os.path.abspath('')

  • 113

    试试这个:

    def get_script_path(for_file = None):
        path = os.path.dirname(os.path.realpath(sys.argv[0] or 'something'))
        return path if not for_file else os.path.join(path, for_file)
    
  • 6
    import os
    exec_filepath = os.path.realpath(__file__)
    exec_dirpath = exec_filepath[0:len(exec_filepath)-len(os.path.basename(__file__))]
    

相关问题