[Tutor] losing info

Karl Pflästerer sigurd at 12move.de
Sat Feb 14 19:32:54 EST 2004


On 15 Feb 2004, Christopher Spears <- cspears2002 at yahoo.com wrote:

> I have run up against a brick wall! I wrote a function
> called get_size(n,directory,files):

[Code]

[...]
> OSError: [Errno 2] No such file or directory:
> 'DSCN4432.JPG'

[...]
> However, the part of the function that is supposed to
> assemble a dictionary of sizes and filenames is not
> working for the directory underneath the current
> directory. Somehow the list of filenames is getting
> lost! Hence the file DSCN4432.JPG cannot be found! 
> What could be causing this?

Without having totally debugged your code; you semm to work with with
only the filenames but not with the path to the files.  So your function
looks in the directory were it was called for the .jpg file and can't
find it.

Again a different approach for your problem (it seems my first version
didn't please you).  It uses a simpler version and the newer os.walk()
instead of os.path.walk().  os.walk seems absolutely right for your
problem (it even returns only a list of filenames so you don't have to
filter the directories only the links).

def get_size(d, n):
    files_size = {}
    for path, dirs, files in os.walk(d):
        flist = [(f, os.path.getsize(os.path.join(path, f)))
                            for f in files if not os.path.islink(f)]
        fcopy = flist[:]
        fcopy.sort(lambda e1, e2: cmp(e1[1], e2[1]))
        files_size[path] = (flist, fcopy[-n:])
        for f in fcopy[-n:]:
            print f
    return files_size


The function returns a hash table with the directory names as keys and
a tuple with the list of corresponding files and the list of the `n'
biggest files as entries.

Perhaps instead of putting only the file name in the list put
os.path.join(path, f) in the list.

The sorting is done with a custom sorting function which looks at the
second element of a tuple.  Instead you could put the file size as first
entry in the tuple and the file name as second entry.  Then the sorting
according to size can be done with a simple sort().

A similar approach is also possible with os.path.walk but os.walk seems
here IMO better suited and easier to use.



   Karl
-- 
Please do *not* send copies of replies to me.
I read the list




More information about the Tutor mailing list