Last active
August 29, 2015 14:28
-
-
Save 3rogue/10e74767be6e75f4a436 to your computer and use it in GitHub Desktop.
遍历目录下文件,按文件大小排序输出到txt中
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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