我正在尝试使用此StackOverflow答案中给出的代码。但是,我不明白那行level = root.replace(startpath, '').count(os.sep)应该做的。另外,当我运行代码时,它ValueError: zero length field name in format在行上给出了一个错误print('{}{}/'.format(indent, os.path.basename(root)))
2 回答

郎朗坤
TA贡献1921条经验 获得超9个赞
这里:
root.replace(startpath, '').count(os.sep)
这root
是步行的当前目录。
root.replace(startpath, '')
startpath
从中删除root
以获得相对于起始路径的路径。
root.replace(startpath, '').count(os.sep)
计算此相对路径内的os.sep
s数量,例如/
对于Linux。此计数是当前目录相对于起始路径的深度。
添加回答
举报
0/150
提交
取消