import os
def search_dir(dir,topdown=True):
files = {}
for root, dirs, files in os.walk(dir, topdown):
for name in files:
fileAdd = os.path.join(root,name)
fileSize = os.path.getsize(fileAdd)
files[fileAdd] = str(fileSize);
print (fileAdd + ', Size:' + str(fileSize)+'kb')
for name in dirs:
fileAdd = os.path.join(root,name)
fileSize = os.path.getsize(fileAdd)
files[fileAdd] = fileSize;
print (fileAdd + ', Size:'+ str(fileSize)+'kb')
dir = raw_input('please input the path:')
search_dir(dir)
for fileAdd in sorted(files):
print("{0} size {1}kb".format(fileAdd, files[fileAdd]))
终端说“列表索引必须是整数而不是 str”,我不知道为什么?