感觉这个没什么好说的:
import os
def rev(path: str, i=0):
s = ‘+‘ * i + path.split(‘/‘)[-1] + ‘\n‘
for _ in os.listdir(path):
if os.path.isdir(path + ‘/‘ + _):
s += rev(path + ‘/‘ + _, i + 1) + ‘\n‘
elif os.path.isfile(path + ‘/‘ + _):
s += ‘+‘ + ‘+‘ * i + _ + ‘\n‘
return s
print(rev(‘D:/PycharmProjects‘))
原文:https://www.cnblogs.com/darkchii/p/12757890.html