下面获取文件,但是没有排序。
for fn in os.listdir(path):
if fn[0] == '.':
continue
try:
p = os.path.join(path, fn)
except:
continue
s = os.lstat(p)
if stat.S_ISDIR(s.st_mode):
l.append((fn, build_tree(p)))
elif stat.S_ISREG(s.st_mode):
l.append((fn, s.st_size))
最佳答案
一种方式
>>> import operator
>>> import os
>>> getall = [ [files, os.path.getsize(files)] for files in os.listdir(".") ]
>>> sorted(getall, key=operator.itemgetter(1))
关于python - 我想用 Python 列出一个目录,然后按大小对文件名进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5575706/