简体   繁体   English

如何在 os walk Python 2.7 中跳过目录

[英]How to skip directories in os walk Python 2.7

I have written an image carving script to assist with my work.我写了一个图像雕刻脚本来协助我的工作。 The tool carves images by specified extention and compares to a hash database.该工具按指定的范围雕刻图像并与哈希数据库进行比较。

The tool is used to search across mounted drives, some which have operating systems on.该工具用于搜索已安装的驱动器,其中一些驱动器装有操作系统。

The problem I am having is that when a drive is mounted with an OS, it is searching across the 'All Users' directory, and so is including images from my local disc.我遇到的问题是,当驱动器安装有操作系统时,它会在“所有用户”目录中搜索,因此包括我本地磁盘中的图像。

I can't figure out how to skip the 'All Users' directory and just stick to the mounted drive.我不知道如何跳过“所有用户”目录并坚持安装的驱动器。

My section for os.walk is as follows:我的 os.walk 部分如下:

for path, subdirs, files in os.walk(root):
    for name in files:
        if re.match(pattern, name.lower()):
                appendfile.write (os.path.join(path, name))
                appendfile.write ('\n')
                log(name)
                i=i+1

Any help is much appreciated任何帮助深表感谢

Assuming All Users is the name of the directory, you can remove the directory from your subdirs list, so that os.walk() does not iterate over it.假设All Users是目录的名称,您可以从subdirs列表中删除该目录,以便os.walk()不会对其进行迭代。

Example -例子 -

for path, subdirs, files in os.walk(root):
    if 'All Users' in subdirs:
        subdirs.remove('All Users')
    for name in files:
        if re.match(pattern, name.lower()):
                appendfile.write (os.path.join(path, name))
                appendfile.write ('\n')
                log(name)
                i=i+1

If you only want to not walk for All Users inside a particular parent, you can include the check for that as well in the above if condition.如果您只想为特定父级内的All Users行走,您也可以在上述if条件中包括检查。

From os.walk documentation -来自os.walk文档-

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

Generate the file names in a directory tree by walking the tree either top-down or bottom-up.通过自顶向下或自底向上遍历树来生成目录树中的文件名。 For each directory in the tree rooted at directory top (including top itself), it yields a 3-tuple (dirpath, dirnames, filenames).对于以目录 top 为根的树中的每个目录(包括 top 本身),它产生一个 3 元组(目录路径、目录名、文件名)。

When topdown is True, the caller can modify the dirnames list in-place (perhaps using del or slice assignment), and walk() will only recurse into the subdirectories whose names remain in dirnames;当 topdown 为 True 时,调用者可以就地修改 dirnames 列表(可能使用 del 或 slice 赋值),而 walk() 只会递归到名称保留在 dirnames 中的子目录; this can be used to prune the search, impose a specific order of visiting, or even to inform walk() about directories the caller creates or renames before it resumes walk() again.这可用于修剪搜索,施加特定的访问顺序,甚至在调用者再次恢复 walk() 之前通知 walk() 有关调用者创建或重命名的目录。 Modifying dirnames when topdown is False is ineffective, because in bottom-up mode the directories in dirnames are generated before dirpath itself is generated. topdown 为 False 时修改 dirnames 是无效的,因为在自底向上模式下,dirnames 中的目录是在生成 dirpath 本身之前生成的。

topdown is normally true, unless specified otherwise.除非另有说明,否则topdown通常为真。

if you have more than one directory to remove you can use a slice-assignment in oder to remove excluded directories in the subdirs如果您要删除多个目录,则可以在 oder 中使用切片分配来删除subdirs中排除的目录

excl_dirs = {'All Users', 'some other dir'}

for path, dirnames, files in os.walk(root):
    dirnames[:] = [d for d in dirnames if d not in excl_dirs]
    ...

as the documentation states:正如文档所述:

When topdown is True , the caller can modify the dirnames list in-place (perhaps using del or slice assignment), and walk() will only recurse into the subdirectories whose names remain in dirnames ;topdownTrue ,调用者可以dirnames修改dirnames列表(可能使用del或 slice 赋值),而walk()只会递归到名称保留在dirnames的子目录; .. ..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM