Python 遍历指定目录下的文件

目录

列出指定目录下的全部文件,或可以通过扩展名指定文件类型,也可以通过指定排除规则,忽略部分文件。

def list_files(
    dir: str, ext: list = None, recursive: bool = True, excludes: list = None
):
    """
    Args:
        - dir, directory path.
        - ext, file extension list, lowercase letters. Default is None, which means all files.
        - recursive, Default is True, will list files in subfolders.
        - excludes, exclude folder or file name list, regexp pattern string. Default is None, which means all folders

    Tips:
        - How to get relative path of a file: os.path.relpath(file_path, dir_path)
        - How to get only name of a file: os.path.basename(file_path)

    Version:
        v0.2.0
        https://gist.github.com/nodewee/eae12e2b74beb82162b8b488648f1fdd
    """

    for f in os.scandir(dir):
        if excludes:
            is_ignore = False
            for pat in excludes:
                if re.search(pat, f.name):
                    is_ignore = True
                    break
            if is_ignore:
                continue

        if recursive:
            if f.is_dir():
                for item in list_files(f.path, ext, recursive, excludes):
                    yield item

        if f.is_file():
            if ext is None:
                yield f.path
            else:
                if os.path.splitext(f.name)[1].lower() in ext:
                    yield f.path

Code on GitHub Gist