gpt4 book ai didi

python - 获取目录的树结构

转载 作者:太空宇宙 更新时间:2023-11-04 10:46:32 27 4
gpt4 key购买 nike

我试图使用 this StackOverflow answer 中给出的代码.但是,我不明白这行是什么

level = root.replace(startpath, '').count(os.sep)

应该做的。

此外,当我运行代码时,它在 ValueError: zero length field name in format 行上给出了错误

print('{}{}/'.format(indent, os.path.basename(root)))

最佳答案

level = root.replace(startpath, '').count(os.sep)

它正在计算打印对象(目录/文件)名称的缩进级别。它正在摆脱 startpath,因为它对于每个列出的文件都很常见,并且让所有内容都缩进 +10 个制表符看起来很糟糕 :) os.sep 在 Linux 上返回路径分隔符,如“/”。

关于那个错误,试试:print('{0}{1}/'.format(indent, os.path.basename(root)))你有一些例子:http://docs.python.org/2/library/string.html#format-examples可能你的 Python 不是 2.7+

关于python - 获取目录的树结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16930030/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com