pathlib。链接断开的路径
我已经使用pathlib.path
模块已经有一段时间了,因为这比os.path
更灵活和可读。到目前为止,我还没有发现PATH
没有涵盖的案例。
我有一个目录,其中包含指向目录的链接,我没有访问权限(我不是该组的一部分)。例如:
> ls dir1
dir1/link1 -> /another_dir/file (No access)
root目录(p
对象)包含几个子目录,而损坏的链接可以在子树的各个级别上。
我试图找到这些破裂的链接并删除它们。但是,我找不到正确执行此操作的方法:
如果我使用
rglob
,该函数仅返回有效的文件和目录,因此此代码不会返回损坏的链接:for p.rglob中的f(“*”): 如果不是f.exists(): 打印(f“ {f}不存在”)
如果我使用
iterdir()
,我必须针对每个子目录进行递归进行操作,并且在该的顶部()
函数触发了我需要捕获的异常(enermission> enermission> enermission>)。
>>> p =路径('dir1/link1') >>> p.is_symlink() 真的 >>> p.exists() ... 许可:[ERRNO 13]允许拒绝:'dir1/link1'
因此,我认为这是我将返回os.walk
和os.path.exists()
的情况,因为此功能返回false
对于断开的链接。
for root, _, files in os.walk(str(p)):
for f in files:
filename = os.path.join(root, f)
if os.path.islink(filename) and not os.path.exists(filename):
os.unlink(filename)
在这里正确使用pathlib
的解决方案吗?
I have been using pathlib.Path
modules for some time now, as this is far more flexible and readable than os.path
. I did not find a case not covered by Path
until now.
I have a directory containing links pointing to a directory I have no access (I am not part of this group). For example:
> ls dir1
dir1/link1 -> /another_dir/file (No access)
The root directory (p
object) contains several sub-directories, and broken links can be at various levels of the sub-tree.
I am trying to find these broken links and remove them. However I did not find a way to do it correctly:
If I use
rglob
, the function only returns files and directories which are valid, so this code does not return the broken links:for f in p.rglob("*"): if not f.exists(): print(f"{f} does not exist")
If I use
iterdir()
, I have to do it recursively for each sub-directory, and on top of thatexists()
function triggers an exception (PermissionError
) that I need to catch.>>> p = Path('dir1/link1') >>> p.is_symlink() True >>> p.exists() ... PermissionError: [errno 13] Permission Denied: 'dir1/link1'
So I think this is case for which I will go back to os.walk
and os.path.exists()
as this function returns False
for broken links.
for root, _, files in os.walk(str(p)):
for f in files:
filename = os.path.join(root, f)
if os.path.islink(filename) and not os.path.exists(filename):
os.unlink(filename)
Any solution to use pathlib
correctly here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因此,与评论一致,我可以找到
pathlib
的最好的方法。两个
打印
语句可以用操作替换(例如os.unlink
)。尽管大多数时候我更喜欢使用
pathlib
而不是os.path
,在这种情况下,os.path
实现要容易得多。写:So the best I can find with
pathlib
is the following, in line with the comments.The two
print
statements can be replaced by an action (Likeos.unlink
).Although most of the time I prefer to use
pathlib
instead ofos.path
, in this case theos.path
implementation is a lot easier to write: