【Python】列出指定目录下的所有文件,可设置扫描深度

还是前些日子遇到的问题,我需要一个小功能来列出指定目录下的所有文件路径,不列出目录路径。

编程语言 Python
语言版本 3.5.3
实现功能 列出指定路径下的所有文件,可以控制扫描深度
程序托管 Github
程序链接 get_paths.py
修改日期 2018年9月19日

程序源码:

# -*- coding: utf-8 -*-

import os


def get_paths(dir_path, depth=None):
    """
    生成器。
    遍历指定目录下的所有非目录文件, 不会列出目录路径。
    注意:这里的路径使用'/'。
    :param dir_path: str/要遍历的目录路径
    :param depth: int/扫描的深度 0:当前目录,1:当前目录的下一级目录
    :return: str/文件路径, 若当前深度下为发现文件,则不返回。
    """
    depth_count = 0

    depth = int(depth) if depth else 0
    dir_path = dir_path if dir_path.endswith('/') else dir_path + '/'

    for path in os.listdir(dir_path):
        tmp_path = dir_path + path

        if os.path.isdir(tmp_path):
            if depth_count < depth:
                depth_count += 1
                yield from get_paths(tmp_path + '/', depth - 1)

        else:
            yield tmp_path


if __name__ == "__main__":
    the_path = './'
    for i in get_paths(the_path, 2):
        print(i)

猜你喜欢

转载自blog.csdn.net/maixiaochai/article/details/82778832