Python 为什么要同时使用 os.path.abspath 和 os.path.realpath?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37863476/
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-19 20:01:28  来源:igfitidea点击:

Why would one use both, os.path.abspath and os.path.realpath?

python

提问by hch

In multiple open source projects, I have seen people do os.path.abspath(os.path.realpath(__file__))to get the absolute path to the current file.

在多个开源项目中,我看到人们这样做是os.path.abspath(os.path.realpath(__file__))为了获取当前文件的绝对路径。

However, I find that os.path.abspath(__file__)and os.path.realpath(__file__)produce the same result. os.path.abspath(os.path.realpath(__file__))seems to be a bit redundant.

但是,我发现os.path.abspath(__file__)os.path.realpath(__file__)产生了相同的结果。os.path.abspath(os.path.realpath(__file__))似乎有点多余。

Is there a reason people are using that?

人们是否有理由使用它?

采纳答案by kdopen

os.path.realpathderefences symbolic links on those operating systems which support them.

os.path.realpath在支持它们的操作系统上取消引用符号链接。

os.path.abspathsimply removes things like .and ..from the path giving a full path from the root of the directory tree to the named file (or symlink)

os.path.abspath简单地从路径中删除诸如...之类的东西,给出从目录树的根到命名文件(或符号链接)的完整路径

For example, on Ubuntu

例如,在 Ubuntu 上

$ ls -l
total 0
-rw-rw-r-- 1 guest guest 0 Jun 16 08:36 a
lrwxrwxrwx 1 guest guest 1 Jun 16 08:36 b -> a

$ python
Python 2.7.11 (default, Dec 15 2015, 16:46:19) 
[GCC 4.8.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.

>>> from os.path import abspath, realpath

>>> abspath('b')
'/home/guest/play/paths/b'

>>> realpath('b')
'/home/guest/play/paths/a'

Symlinks can contain relative paths, hence the need to use both. The inner call to realpathmight return a path with embedded ..parts, which abspaththen removes.

符号链接可以包含相对路径,因此需要同时使用两者。内部调用realpath可能会返回一个带有嵌入..部分的路径,abspath然后将其删除。

回答by jobrad

For your stated scenario, there is no reason to combine realpath and abspath, since os.path.realpathactually calls os.path.abspathbefore returning a result (I checked Python 2.5 to Python 3.6).

对于您所述的场景,没有理由将 realpath 和 abspath 结合起来,因为在返回结果之前os.path.realpath实际调用os.path.abspath了(我检查了 Python 2.5 到 Python 3.6)。

  • os.path.abspathreturns the absolute path, but does NOT resolve symlinks in its argument.
  • os.path.realpathwill first resolve any symbolic links in the path, and then return the absolute path.
  • os.path.abspath返回绝对路径,但不解析其参数中的符号链接。
  • os.path.realpath将首先解析路径中的任何符号链接,然后返回绝对路径。

However, if you expect your path to contain a ~, neither abspath or realpath will resolve ~to the user's home directory, and the resulting path will be invalid. You will need to use os.path.expanduserto resolve this to the user's directory.

但是,如果您希望路径包含 a ~,则abspath 或 realpath 都不会解析~到用户的主目录,并且生成的路径将是 invalid。您将需要使用os.path.expanduser将其解析为用户目录。

For the sake of a thorough explanation, here are some results which I've verified in Windows and Linux, in Python 3.4 and Python 2.6. The current directory (./) is my home directory, which looks like this:

为了彻底解释,以下是我在 Windows 和 Linux、Python 3.4 和 Python 2.6 中验证的一些结果。当前目录 ( ./) 是我的主目录,如下所示:

myhome
|- data (symlink to /mnt/data)
|- subdir (extra directory, for verbose explanation)
# os.path.abspath returns the absolute path, but does NOT resolve symlinks in its argument
os.path.abspath('./')
'/home/myhome'
os.path.abspath('./subdir/../data')
'/home/myhome/data'


# os.path.realpath will resolve symlinks AND return an absolute path from a relative path
os.path.realpath('./')
'/home/myhome'
os.path.realpath('./subdir/../')
'/home/myhome'
os.path.realpath('./subdir/../data')
'/mnt/data'

# NEITHER abspath or realpath will resolve or remove ~.
os.path.abspath('~/data')
'/home/myhome/~/data'

os.path.realpath('~/data')
'/home/myhome/~/data'

# And the returned path will be invalid
os.path.exists(os.path.abspath('~/data'))
False
os.path.exists(os.path.realpath('~/data'))
False

# Use realpath + expanduser to resolve ~
os.path.realpath(os.path.expanduser('~/subdir/../data'))
'/mnt/data'

回答by ExtractTable.com

In the layman terms, if you are trying to get the path of a shortcut file, absolute path gives the complete path of the file present in the shortcut location, while realpath gives the original locationpath of the file.

通俗地说,如果您尝试获取快捷方式文件的路径,则绝对路径给出快捷方式 location 中存在的文件的完整路径,而 realpath 给出文件的原始位置路径。

Absolute path, os.path.abspath(), gives the complete path of the file which is located in the current working directory or the directory you mentioned.

绝对路径 os.path.abspath() 给出位于当前工作目录或您提到的目录中的文件的完整路径。

Real path, os.path.realpath(), gives the complete path of the file which is being referred.

真实路径 os.path.realpath() 给出了被引用文件的完整路径。

Eg:

例如:

file = "shortcut_folder/filename"
os.path.abspath(file) = "C:/Desktop/shortcut_folder/filename"
os.path.realpath(file) = "D:/PyCharmProjects/Python1stClass/filename"