pathlib。链接断开的路径

发布于 2025-02-01 00:43:44 字数 1291 浏览 2 评论 0原文

我已经使用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.walkos.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 that exists() 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最笨的告白 2025-02-08 00:43:44

因此,与评论一致,我可以找到pathlib的最好的方法。

for dir in p.rglob("**/"):
    for filename in dir.iterdir():
        if filename.is_symlink():
            try:
                if not filename.exists():
                    print(f"{filename} is a broken link")
            except PermissionError:
                print(f"{filename} links to an file we cannot access")

两个打印语句可以用操作替换(例如os.unlink)。

尽管大多数时候我更喜欢使用pathlib而不是os.path,在这种情况下,os.path实现要容易得多。写:

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)

So the best I can find with pathlib is the following, in line with the comments.

for dir in p.rglob("**/"):
    for filename in dir.iterdir():
        if filename.is_symlink():
            try:
                if not filename.exists():
                    print(f"{filename} is a broken link")
            except PermissionError:
                print(f"{filename} links to an file we cannot access")

The two print statements can be replaced by an action (Like os.unlink).

Although most of the time I prefer to use pathlib instead of os.path, in this case the os.path implementation is a lot easier to write:

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文