import sys
import os
def dir_grade(dir,child_dir):
if dir==child_dir:
return 0
if dir not in child_dir:
return 0
if dir==os.path.split(child_dir)[0]:
return 1
return 1+dir_grade(dir, os.path.split(child_dir)[0])
def read_files(al_path):
retract=" "#缩进原始长短
for root, dirs, files in os.walk(al_path):
grade=dir_grade(al_path,root)
#定义缩进长短
str_L=retract*grade
print() #输出换行
print(str_L+str(grade)+"级目录"+":"+os.path.basename(root))
# print(dirss)
#打印改地址下的目录
for dir in dirs:
print(str_L+"---"+dir)#使用“---”标记目录
#打印该地址下的文件
for file in files:
print(" "+str_L+file)
# print(dirs, files)
if __name__ == '__main__':
f_num, sum_num = 0, 0
for i in range(0, len(sys.argv)):
print("i= %d path = %s" % (i, sys.argv[i]))
if i == 0:
while True:
dir_name = input("请输入目录地址:")
read_files(dir_name)
else:
for zwy in range(1,i+1):
print("start")
read_files(sys.argv[zwy])
python使用递归缩进 打印目录树
最新推荐文章于 2022-03-23 13:50:07 发布