Newbie lists question

Wolfe Maykut wolfe at
Tue Aug 14 00:17:57 CEST 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?




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] = []

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