Reputation: 667
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]))
The terminal says "list indices must be integers not str" and I do not know why?
Upvotes: 0
Views: 666
Reputation: 77347
You used the 'files' variable twice.
files = {}
for root, dirs, files in os.walk(dir, topdown):
One of them has got to go.
(Rant mode: ON)
This sort of thing is very easily found when using a debugger.
(Rant mode: OFF)
Upvotes: 2