os.walk without digging into directories below

前端 未结 20 1288
庸人自扰
庸人自扰 2020-12-04 06:21

How do I limit os.walk to only return files in the directory I provide it?

def _dir_list(self, dir_name, whitelist):
    outputList = []
    for         


        
20条回答
  •  庸人自扰
    2020-12-04 06:55

    create a list of excludes, use fnmatch to skip the directory structure and do the process

    excludes= ['a\*\b', 'c\d\e']
    for root, directories, files in os.walk('Start_Folder'):
        if not any(fnmatch.fnmatch(nf_root, pattern) for pattern in excludes):
            for root, directories, files in os.walk(nf_root):
                ....
                do the process
                ....
    

    same as for 'includes':

    if **any**(fnmatch.fnmatch(nf_root, pattern) for pattern in **includes**):
    

提交回复
热议问题