欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

【递归遍历01】遍历目录(只需填写路径,便可以得到该路径下的所有文件!!!)

程序员文章站 2022-05-03 20:05:09
...
import os


def getAllDir(path):
    # 得到当前目录下所有的文件
    fileList = os.listdir(path)
    print(fileList)
    # 处理每一个文件 目录继续向下寻找 文件停止
    for fileName in fileList:
        # 判断是否是路径
        fileAbsPath = os.path.join(path, fileName)
        if os.path.isdir(fileAbsPath):
            print('目录', fileName)
            getAllDir(fileAbsPath)
        else:
            print('普通文件', fileName)


path = 'D:\pypypy'

getAllDir(path)

只需要修改path,可以遍历任何目录。

相关标签: python之道