[Tutor] thanks!
Christopher Spears
cspears2002 at yahoo.com
Sat Feb 14 20:25:18 EST 2004
I solved the problem! As several have pointed out, I
was providing file names not pathways. Here is the
resulting code:
import os
def get_size(n,directory,files):
files_of_size = {}; sizes = [];
biggest_files = [];
files = map(os.path.join,[directory] *
len(files),files)
files = filter(lambda x: not os.path.isdir(x)
and not os.path.islink(x), files)
#Creates a list of file pathnames.
for f in files:
files_of_size.setdefault(os.path.getsize(f),[]).append(f)
#Creates a dictionary where the keys are the sizes.
sizes = files_of_size.keys()
sizes.sort(); sizes.reverse();
for s in sizes:
biggest_files.append([files_of_size[s],s])
#Sorts keys from largest to smallest and places the
names in a
#new list in that order.
print "in directory %s:" % directory
try:
while n > 0:
print biggest_files[n-1]
n = n - 1
except IndexError,msg:
print "%d is too high. Try a smaller
number" %n
#Prints out the n largest files.
Notice I do not use a lot of functional programming.
I have nothing against functional programming. I just
do not undertand much of it, which is why I took this
particular approach. Karl has pointed out several
times that if I used functional programming then my
code would be much more succinct.
Thanks!
More information about the Tutor
mailing list