如何在 Python 中获取符号链接目标?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33232417/
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
How to get symlink target in Python?
提问by zorggy
Using Python, I need to check whether hundreds of symlinks are correct and recreate them when not. What I do now is to compare real paths of what I want and what I have, but it's slow because it's over NFS with an automount.
使用 Python,我需要检查数百个符号链接是否正确,如果不正确,则重新创建它们。我现在要做的是比较我想要的和我拥有的真实路径,但它很慢,因为它是通过自动挂载的 NFS。
Otherwise I'm going to run a subprocess with the command 'ls -l' and work on the list of strings returned. I would prefer a better solution, using a Python library...
否则,我将使用命令 'ls -l' 运行一个子进程并处理返回的字符串列表。我更喜欢更好的解决方案,使用 Python 库...
Edit1:I have: link_name -> link_target
and then link_target -> a_real_file
. What I need is to extract link_target
from link_name
, not a_real_file
. I don't care if the real file does not exist.
编辑 1:我有:link_name -> link_target
然后link_target -> a_real_file
. 我需要的是提取link_target
自link_name
,没有a_real_file
。我不在乎真实文件是否存在。
Edit2:Maybe I did not express correctly. What I mean by a correct symlink is 'a link that point to a predefined path, even if it does not exist'. So I need to check that:
Edit2:可能我表达的不正确。我所说的正确符号链接是“指向预定义路径的链接,即使它不存在”。所以我需要检查一下:
link_name_1 -> target_1
link_name_2 -> target_2
That's why I need to extract targets, not the real paths. Then I compare them to a reference (dictionary). So my question is: How do I extract the target path?
这就是为什么我需要提取目标,而不是真正的路径。然后我将它们与参考(字典)进行比较。所以我的问题是:如何提取目标路径?
回答by zorggy
To determine if a directory entry is a symlink use this:
要确定目录条目是否是符号链接,请使用以下命令:
Return True if path refers to a directory entry that is a symbolic link. Always False if symbolic links are not supported.
如果路径指的是作为符号链接的目录条目,则返回 True。如果不支持符号链接,则始终为 False。
回答by AChampion
To determine if a link is broken, you can, os.walk
and test os.path.exists(path)
which will return False for a broken link. You can then use os.path.realpath(path)
to find out what the link is supposed to be pointing to.
Something like (untested):
要确定链接是否已损坏,您可以os.walk
测试os.path.exists(path)
哪个将为损坏的链接返回 False。然后,您可以使用它os.path.realpath(path)
来找出链接应该指向的内容。
类似的东西(未经测试):
for root, dirs, files in os.walk('<path>'):
for file in files:
f = os.join(root, file)
if os.path.islink(f) and not os.path.exists(f):
print("Broken: {} -> {}".format(f, os.path.realpath(f)))
回答by Armin Rigo
You need to look at os.readlink()
.
你需要看看os.readlink()
。
回答by wim
The problem with os.readlink()
is it will only resolve 1 step of the link. We can have a situation where A
links to another link B
, and B
link is dangling.
问题os.readlink()
是它只能解决链接的 1 步。我们可能会遇到A
链接到另一个链接的情况B
,并且B
链接悬空。
$ ln -s /tmp/example/notexist /tmp/example/B
$ ln -s /tmp/example/B /tmp/example/A
$ ls -l /tmp/example
A -> /tmp/example/B
B -> /tmp/example/notexist
Now in Python, os.readlink
gives you the first target.
现在在 Python 中,os.readlink
为您提供第一个目标。
>>> import os
>>> os.readlink('A')
'/tmp/example/B'
But in most situations I assume we are interested in the resolved path. So pathlib
can help here:
但在大多数情况下,我认为我们对已解决的路径感兴趣。所以pathlib
可以在这里提供帮助:
>>> from pathlib import Path
>>> Path('A').resolve()
PosixPath('/tmp/example/notexist')
For older Python versions:
对于较旧的 Python 版本:
>>> os.path.realpath('A')
'/tmp/example/notexist'