Skip to content

Instantly share code, notes, and snippets.

@3rogue
Last active August 29, 2015 14:28
Show Gist options
  • Save 3rogue/10e74767be6e75f4a436 to your computer and use it in GitHub Desktop.
Save 3rogue/10e74767be6e75f4a436 to your computer and use it in GitHub Desktop.
遍历目录下文件,按文件大小排序输出到txt中
import os
def list_size(path):
res={}
for dirpath, dirnames, filenames in os.walk(path):
for name in filenames:
fullpath = os.path.join(dirpath, name)
size = os.path.getsize(fullpath)
res[fullpath] = size/1024/1024
return sorted(res.items(), key = lambda d:d[1], reverse = True)
path = 'E:'
with open ('1.txt','w') as f:
for i in list_size(path):
f.write(i[0]+'\t'+str(i[1])+'MB'+'\n')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment