如何获取所有子目录和文件的列表以及按大小排序的大小?

Rom*_*aan 4 python

如何获取所有子目录和文件的列表以及按大小按升序排序的大小?

下面的代码获取了所有文件的列表,但没有根据大小排序.请帮忙.

import os
import os.path, time
from os.path import join, getsize
count=0
for root, dirs, files in os.walk('Test'):
    for file in list(files):
        fileaddress = os.path.join(root, file)
        print("\nName:",fileaddress)
        print("Time:",time.strftime("%m/%d/%Y %I:%M:%S %p",time.localtime(os.path.getmtime(fileaddress))))
        count=count+1
print(count);
Run Code Online (Sandbox Code Playgroud)

mgi*_*son 5

import os
from os.path import join, getsize

file_list = []
for root, dirs, files in os.walk('Test'):
    file_list.extend( join(root,f) for f in files )
    #May be *slightly* faster at the expense of a little readability 
    # and a little memory
    # file_list.extend( [ join(root,f) for f in files ] ) 


print (sorted(file_list, key=getsize))
Run Code Online (Sandbox Code Playgroud)

同样的事情dirs- 虽然我不确定目录的"大小"实际上是什么 - 你可能无法对那个进行排序getsize(如果可以的话,你将不会得到一个结果有意义的).