【发布时间】:2020-11-12 09:18:54
【问题描述】:
def list_files(startpath):
for root, dirs, files in os.walk(startpath):
# print(dirs)
if dir!= '.git':
level = root.replace(startpath, '').count(os.sep)
indent = ' ' * 4 * (level)
print('{}{}/'.format(indent, os.path.basename(root)))
subindent = ' ' * 4 * (level + 1)
for f in files:
print('{}{}'.format(subindent, f))
我不明白 level = root.replace(startpath, '').count(os.sep) 是如何工作的,请详细解释一下。
【问题讨论】:
-
os.sep是一种跨平台获取路径分隔符的方式。 -
你知道
replace是做什么的吗?你知道count做什么吗?你知道os.sep是什么吗? -
来自文档 -
os.sep- 操作系统用来分隔路径名组件的字符。这是 '/' 对于 POSIX 和 '\\' 对于 Windows。 -
查看 python count() 的文档。
标签: python file operating-system