使用 os.walk 时跳过某个文件夹

Skip a certain folder when using os.walk

这是我的代码:

rootdir_path_without_slash = '/home/winpc/Downloads/Prageeth/backups/Final/node-wen-app'
rootdir_path_with_slash= '/home/winpc/Downloads/Prageeth/backups/Final/node-wen-app/'


dir_src = (rootdir_path_with_slash)
for subdir, dirs, files in os.walk(rootdir_path_without_slash):
    for file in files:
        file_name=os.path.join(subdir, file)
        if file_name.endswith('.html'):
            print file_name

此处代码导航给定源目录中的所有子目录以进行搜索。html file.I 如果节点模块文件夹 found.Please 帮助我,则需要跳过。

您需要在 根目录 上放置一个 if 条件,以避免遍历 node_modules 或其任何后代。你会想要:

for subdir, dirs, files in os.walk(rootdir_path_without_slash):
    if 'node_modules' in subdir:
        continue
    ... # rest of your code

另外,subdir这里用词不当,第一个参数os.walk returns是根路径