中心思想:递归函数
代码如下
import os
import os.path
def tree(path, depth=0):
if depth == 0:
print(path)
items = os.listdir(path)
for item in items:
# 输出文件夹中的文件和子文件夹名
print('| '*depth, end='')
print('|----', item)
item = os.path.join(path, item)
if os.path.isdir(item):
# 递归遍历子目录
tree(item, depth+1)
tree(r'E:\pycharm\pycharm2020.3\pycharm2020.3\cjh\learn_code')
运行结果