【发布时间】:2013-08-07 14:32:54
【问题描述】:
我从这里得到了部分答案Construct a tree from list os file paths (Python) - Performance dependent
我的具体问题需要我从
这个
dir/file 10
dir/dir2/file2 20
dir/dir2/file3 10
dir/file3 10
dir3/file4 10
dir3/file5 10
到
dir/ **50**
dir2/ **30**
file2
file3
file
file3
dir3/ **20**
file4
file5
基本上最后的数字是文件大小和
我一直在试图弄清楚如何将所有文件的大小显示到父目录
编辑:
r = re.compile(r'(.+\t)(\d+)')
def prettify(d, indent=0):
for key, value in d.iteritems():
ss = 0
if key == FILE_MARKER:
if value:
for each in value:
mm = r.match(each)
ss += int(mm.group(2))
print ' ' * indent + each
***print ' ' * indent + format_size(ss)***
else:
print ' ' * indent + str(key)
if isinstance(value, dict):
addSizes(value, indent+1)
else:
print ' ' * (indent+1) + str(value)
这是我使用 regExp 编辑的上述链接中的 mac 答案
我想到的解决方案让我创建了一个新的 dict 或添加了一个内部函数。
我已经失去了一整天,希望我能在当天早些时候寻求帮助。
请帮忙。
【问题讨论】:
-
@larsks 我可以将大小相加并将其显示在每个目录之后的文件列表之后,但坚持在 dir/ 行上显示它。
-
您应该向我们展示您的尝试,然后我们可以提供帮助
标签: python dictionary