Sorting directory contents

Wolfgang Draxinger wdraxinger at darkstargames.de
Tue Feb 20 08:35:25 EST 2007


H folks,

I got, hmm not really a problem, more a question of elegance:

In a current project I have to read in some files in a given
directory in chronological order, so that I can concatenate the
contents in those files into a new one (it's XML and I have to
concatenate some subelements, about 4 levels below the root
element). It all works, but somehow I got the feeling, that my
solution is not as elegant as it could be:

src_file_paths = dict()
for fname in os.listdir(sourcedir):
        fpath = sourcedir+os.sep+fname
        if not match_fname_pattern(fname): continue
        src_file_paths[os.stat(fpath).st_mtime] = fpath
for ftime in src_file_paths.keys().sort():
        read_and_concatenate(src_file_paths[ftime])

of course listdir and sorting could be done in a separate
function, but I wonder if there was a more elegant approach.

Wolfgang Draxinger
-- 
E-Mail address works, Jabber: hexarith at jabber.org, ICQ: 134682867




More information about the Python-list mailing list