【递归遍历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,可以遍历任何目录。