os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir)) 是什么意思?Python

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21005822/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 21:46:36  来源:igfitidea点击:

What does os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir)) mean? python

pythonimportpathoperating-systemdirectory

提问by alvas

In several SO's question there is these lines to access the parent directory of the code, e.g. os.path.join(os.path.dirname(__file__)) returns nothingand os.path.join(os.path.dirname(__file__)) returns nothing

在几个 SO 的问题中有这些行来访问代码的父目录,例如os.path.join(os.path.dirname(__file__)) 不返回任何内容os.path.join(os.path.dirname(__file__) ) 不返回任何内容

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

I understand that os.path.abspath()returns absolute path of something and sys.path.append()adds the path for the code to access. but what is this cryptic line below, what does it really mean?

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

os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir))

Is there another way to achieve the same purpose of appending the parent directory of the where the code?

是否有另一种方法可以达到附加代码所在父目录的相同目的?

This problem happens because I am calling functions across directories and sometimes they share the same file name, e.g. script1/utils.pyand script2/utils.py. I am calling a function from script1/test.pywhich calls script2/something.pycontains a function that calls script2/utils.pyand the following code

发生此问题是因为我跨目录调用函数,有时它们共享相同的文件名,例如script1/utils.pyscript2/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()

something.py

东西.py

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()

采纳答案by Paulo Bu

That is a clever way to refer to paths regardless of the script location. The crypticline you're referring is:

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

os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir))

There are 3 methods and a 2 constants present:

有 3 种方法和 2 个常量:

  1. abspathreturns absolute path of a path
  2. joinjoin to path strings
  3. dirnamereturns the directory of a file
  4. __file__refers to the script's file name
  5. pardirreturns the representation of a parent directory in the OS (usually ..)
  1. abspath返回路径的绝对路径
  2. join加入路径字符串
  3. dirname返回文件的目录
  4. __file__指的是script的文件名
  5. pardir返回操作系统中父目录的表示(通常为..

Thus, the expression returns the full path name of the executing scriptin a multiplatform-safeway. No need to hardwireany directions, that's why it is so useful.

因此,该表达式以多平台安全的方式返回执行脚本完整路径名。无需硬连线任何方向,这就是它如此有用的原因。

There might be other approaches to get a parent directory of where a file is located, for example, programs have the concept of current working directory, os.getcwd(). So doing os.getcwd()+'/..'might work. But this is very dangerous, because working directories can be changed.

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

Also, if the file is intended to be imported, the working directory will point to the importing file, not the importee, but __file__always points to the actual module's file so it is safer.

此外,如果要导入文件,工作目录将指向导入文件,而不是导入者,但__file__始终指向实际模块的文件,因此更安全。

Hope this helps!

希望这可以帮助!

Edit: P.S. - Python 3 greatly simplifies this situation by letting us treat paths in an object-oriented manner, so the above line becomes:

编辑:PS - Python 3 通过让我们以面向对象的方式处理路径,大大简化了这种情况,因此上面的行变为:

from pathlib import Path
Path(__file__).resolve().parent.parent

回答by praveen

__file__represents the file the code is executing from

__file__表示代码正在执行的文件

os.path.dirname(__file__)gives you the directory the file is in

os.path.dirname(__file__)给你文件所在的目录

os.path.pardirstands for ".." which means one directory above the current one

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

os.path.join(os.path.dirname(__file__), os.path.pardir)joins the directory name and ".."

os.path.join(os.path.dirname(__file__), os.path.pardir)加入目录名和“..”

os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir))resolves the above path and gives you an absolute path for the parent directory of the directory your file is in

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