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


98

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

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

人们使用它是有原因的吗?

Answers:


66

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

os.path.abspath只需从路径中删除类似.和的东西,即可..提供从目录树的根到命名文件(或符号链接)的完整路径

例如,在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'

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


10
尽管此答案描述了两个函数之间的区别,但是的结果并realpath()不能包含..组件,也没有真正回答为什么要同时使用这两个问题。Jobrad的答案更为准确。
迈尔斯

对于Python 3.7和Windows,realpath无法理解带驱动器的驱动器。对于带驱动器的驱动器,请说P:\,源文件夹为c:\ MyFolder \ Bla,realpath返回P:\而不是c:\ MyFolder \ Bla。任何人都知道如何获得“真实”路径吗?
Totte Karlsson,

对我来说,abspath如果当前目录是符号链接,则仍然遵循符号链接。我只找到了这个答案stackoverflow.com/questions/54665065/…来停止这种行为。
Victor Sergienko

107

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

  • os.path.abspath 返回绝对路径,但不解析其参数中的符号链接。
  • os.path.realpath 将首先解析路径中的任何符号链接,然后返回绝对路径。

但是,如果您期望路径包含a ~,则abspath或realpath都不会解析~到用户的主目录,并且结果路径将无效。您将需要使用os.path.expanduser将此解析到用户目录。

为了全面解释,以下是一些结果,这些结果已在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'

3
真好 为了说明您的观点,您的最后一个例子应该是os.path.realpath(os.path.expanduser('~/subdir/../data'))
Arthur

3
然后,如果在路径中使用了变量,也不要忘记os.path.expandvars
Slavenskij 18'Apr

7

用外行术语来说,如果您尝试获取快捷方式文件的路径,则绝对路径将提供快捷方式位置中存在的文件的完整路径,而realpath则会提供文件的原始位置路径。

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

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

例如:

file = "shortcut_folder/filename"
os.path.abspath(file) = "C:/Desktop/shortcut_folder/filename"
os.path.realpath(file) = "D:/PyCharmProjects/Python1stClass/filename"
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.