Python os.walk +关注符号链接

lin*_*eak 32 python symlink traversal directory-traversal symlink-traversal

如何让这篇文章在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
Run Code Online (Sandbox Code Playgroud)

Ric*_*ook 50

设置followlinksTrue.这是该os.walk方法的第四个参数,转载如下:

os.walk(top[, topdown=True[, onerror=None[, followlinks=False]]])
Run Code Online (Sandbox Code Playgroud)

Python 2.6中添加了此选项.

  • 谢谢,`os.walk(path,followlinks = True):`做了诀窍,虽然Python文档对此非常不清楚:http://docs.python.org/library/os.path.html#os.path .步行 (6认同)
  • @Frank:当然不清楚; 你正在查看`os.path.walk`的文档,它是一个单独的(旧的和已弃用的)函数.您应该查看[`os.walk`](http://docs.python.org/library/os.html#os.walk)文档. (6认同)