Python 递归,遍历文件夹有层次感

import os

#遍历文件夹
def listFilename2(path1,treeshow):  #加入参数treeshow
	fileList=os.listdir(path1)
	for file in fileList:
		mypath=os.path.join(path1,file)  #连接成绝对路径
		if os.path.isdir(mypath):
			print(treeshow+mypath)
			treeshow+="--"    #将treeshow作为参数
			listFilename2(mypath,treeshow)   #递归
		else:
			print(treeshow+mypath)


listFilename2("C:\\aa\\bb","")

你可能感兴趣的:(Python)