Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
os.path.abspath(os.path.join(os.path.dirname(__文件),os.path.pardir))是什么意思?python_Python_Import_Path_Operating System_Directory - Fatal编程技术网

os.path.abspath(os.path.join(os.path.dirname(__文件),os.path.pardir))是什么意思?python

os.path.abspath(os.path.join(os.path.dirname(__文件),os.path.pardir))是什么意思?python,python,import,path,operating-system,directory,Python,Import,Path,Operating System,Directory,在几个SO的问题中,有这些行用于访问代码的父目录,例如和 我知道os.path.abspath()返回某物的绝对路径,sys.path.append()添加代码访问的路径。但是下面这条神秘的线是什么,它到底是什么意思? os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir)) 是否有其他方法可以达到添加代码所在位置的父目录的相同目的 发生此问题的原因是我跨目录调用函数,有时它们共享相同的文件名,例如scri

在几个SO的问题中,有这些行用于访问代码的父目录,例如和

我知道
os.path.abspath()
返回某物的绝对路径,
sys.path.append()
添加代码访问的路径。但是下面这条神秘的线是什么,它到底是什么意思?

os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir))
是否有其他方法可以达到添加代码所在位置的父目录的相同目的

发生此问题的原因是我跨目录调用函数,有时它们共享相同的文件名,例如
script1/utils.py
script2/utils.py
。我正在从
script1/test.py
调用一个函数,该函数调用
script2/something.py
包含一个调用
script2/utils.py
的函数和以下代码

script1/
        utils.py
        src/
            test.py

script2/
        utils.py
        code/
            something.py
test.py

from script2.code import something
import sys
sys.path.append('../')
import utils

something.foobar()
import os, sys
parentddir = os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir))
sys.path.append(parentddir)
import utils

def foobar():
  utils.somefunc()
something.py

from script2.code import something
import sys
sys.path.append('../')
import utils

something.foobar()
import os, sys
parentddir = os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir))
sys.path.append(parentddir)
import utils

def foobar():
  utils.somefunc()

\uuuuu file\uuuu
表示代码从中执行的文件

os.path.dirname(_文件)
提供文件所在的目录

os.path.pardir
代表“.”,即当前目录之上的一个目录

os.path.join(os.path.dirname(\uuuuu文件),os.path.pardir)
连接目录名和“.”


os.path.abspath(os.path.join(os.path.dirname(\uuuu file\uuuu),os.path.pardir))
解析上述路径,并为文件所在目录的父目录提供绝对路径

,这是一种无论脚本位置如何引用路径的巧妙方法。你所指的神秘线是:

os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir))
有3种方法和2个常数:

  • abspath
    返回路径的绝对路径
  • 连接
    连接到路径字符串
  • dirname
    返回文件的目录
  • \uuuu file\uuuu
    指的是
    脚本的文件名
  • pardir
    返回操作系统中父目录的表示形式(通常
  • 因此,表达式以多平台安全的方式返回执行脚本的完整路径名。不需要硬连线任何方向,这就是为什么它如此有用

    可能还有其他方法可以获取文件所在的父目录,例如,程序具有当前工作目录的概念,
    os.getcwd()
    。所以做
    os.getcwd()+“/…”
    可能会奏效。但这是非常危险的,因为工作目录可以更改

    此外,如果要导入文件,则工作目录将指向导入文件,而不是导入对象,但
    \uuuuu file\uuuu
    始终指向实际模块的文件,因此更安全

    希望这有帮助

    Edit:p.S.-Python 3通过让我们以面向对象的方式处理路径,大大简化了这种情况,因此上面的一行变成:

    从pathlib导入路径
    路径(_文件__).resolve().parent.parent
    
    只是一个问题,您是说您需要这样做才能导入UTIL
    ?我没有正确理解最后一部分。您不能使用
    \uuuuu文件\uuuuu
    的de path
    dirname
    ,因为文件只是脚本的名称。您需要使用
    os.getcwd()
    来获取该路径。