I have a script that will walk a system directory, and get the files sizes in that directory. it then sorts by the file size(descending), takes two arguments. The first arg is the system path and the second arg is an integer to limit the output to that integer count.
I’m using a while loop to limit the print output but its getting stuck in a infinite loop…. obviously, something is wrong but i cant see it.
#!/usr/bin/python
import sys
import os
#Grab arguments
mydir = sys.argv[1]
mycnt = sys.argv[2]
print mydir
print mycnt
#set base dir
root_path = mydir
fileSize = 0
rootdir = mydir
filedict = dict()
count = 0
for root, dirs, files in os.walk(rootdir):
for file in files:
filepath = os.path.join(root,file)
filesize = fileSize + os.path.getsize(filepath)
filedict[filepath] = filesize
for key, value in sorted(filedict.iteritems(), key=lambda (key,val): (val,key), reverse=True):
while (count <= mycnt):
print "(%8s)" " (%-8s)" % (value, key)
count += 1
else:
sys.exit()
If
mycntis a string, not an integer (which it is when read directly fromsys.argv), your loop will never end.