PYTHON遍历文件夹指定类型文件

def allfiles(root, patterns = ‘*’, single_level = False, yield_folders = False):
    patterns = patterns.split(‘;’)
    for path, subdirs, files in os.walk(root):
        if yield_folders:
            files.extend(subdirs)
        files.sort()
        for name in files:
            for pattern in patterns:
                if fnmatch.fnmatch(name, pattern):
                    yield os.path.join(path, name)
                    break
        if single_level:
            break

filedir = ‘./’
filetype = ‘*.h;*.c’

list(allfiles(filedir, filetype))

发表评论