Python os.walk +关注符号链接

时间:2010-09-22 16:57:00

标签: 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

1 个答案:

答案 0 :(得分:48)

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

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

此选项已在Python 2.6中添加。