Newbie lists question

Wolfe Maykut wolfe at spam.me.not
Mon Aug 13 18:17:57 EDT 2001


    Hi.  I'm trying to create a list on the end of a dictionary.   I'd
like to have a single loop, but since the dictionary is dynamically
generated it appears as if I have to test to see if the key is around
and then treat each case differently in order to initialize the loop.
It looks ugly.  Is there a nicer way of doing this that I'm missing?

    Thanks,

                --Wolfe

#!/usr/bin/python

import os, time, glob, string

backupdir = glob.glob("/netbackups/.disks/disk*/*.out")

prefixes = {}

for file in backupdir:
    filename = string.split(file, '/')[-1]
    prefix = string.split(filename,'.')[0]
    if prefixes.has_key(prefix):                     # This is the loop
I think is tres' ugly
       prefixes[prefix].append(file)
    else:
        prefixes[prefix] = []
        prefixes[prefix].append(file)

for key in prefixes.keys():
    print key
    for file in prefixes[key]:
        st = os.stat(file)
        format = time.strftime("%b-%d-%Y-%H:00", time.localtime(st[8]))
        print "  ", file, format




More information about the Python-list mailing list