遍历目录

# 方式1:遍历所有文件(包括子目录,递归)
from pathlib import Path
import os


def return_all_file_abspath(p):
    r = Path(p)
    out = []
    print("=== 遍历所有文件 ===")
    for file_path in r.rglob("*"):  # rglob("*") 递归匹配所有文件/目录
        if file_path.is_file():  # 仅筛选文件(排除目录)
            out.append(os.path.abspath(file_path))
    return out


if __name__ == "__main__":

    print(return_all_file_abspath("."))

posted on 2025-12-12 20:21  张博的博客  阅读(3)  评论(0)    收藏  举报

导航