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')