Python os.walk +跟随符号链接

发布于 2024-09-24 04:44:11 字数 374 浏览 3 评论 0原文

如何让这篇文章遵循 python 2.6 中的符号链接?

def load_recursive(self, path):
    for subdir, dirs, files in os.walk(path):
        for file in files:
            if file.endswith('.xml'):
                file_path = os.path.join(subdir, file)
                try:
                    do_stuff(file_path) 
                except:
                    continue

How do I get this piece to follow symlinks in python 2.6?

def load_recursive(self, path):
    for subdir, dirs, files in os.walk(path):
        for file in files:
            if file.endswith('.xml'):
                file_path = os.path.join(subdir, file)
                try:
                    do_stuff(file_path) 
                except:
                    continue

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

╰ゝ天使的微笑 2024-10-01 04:44:11

followlinks 设置为 True。这是 os.walk 方法的第四个参数,复制如下:

os.walk(top[, topdown=True[, onerror=None[, followlinks=False]]])

此选项是在 Python 2.6 中添加的。

编辑1

使用followlinks=True时要小心。根据文档

注意:请注意,将 followlinks 设置为 True 可能会导致
如果链接指向其自身的父目录,则无限递归。
walk() 不会跟踪它已经访问过的目录。

Set followlinks to True. This is the fourth argument to the os.walk method, reproduced below:

os.walk(top[, topdown=True[, onerror=None[, followlinks=False]]])

This option was added in Python 2.6.

EDIT 1

Be careful when using followlinks=True. According to the documentation:

Note: Be aware that setting followlinks to True can lead to
infinite recursion if a link points to a parent directory of itself.
walk() does not keep track of the directories it visited already.

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