import os
path = 'C:\\aa'
for root,dirs,files in os.walk(path):
print("Root=",root,'dirs=',dirs,'files =',files)

执行结果:

Root= C:\aa dirs= ['bb'] files = ['b1.txt', 'b2.txt']
Root= C:\aa\bb dirs= ['cc'] files = ['cc1.txt', 'cc2.txt']
Root= C:\aa\bb\cc dirs= ['dd'] files = ['d1.txt', 'd2.txt', '新建 RTF 文档.rtf']
Root= C:\aa\bb\cc\dd dirs= [] files = []
05-08 08:15