Wolfgang Draxinger wrote: > 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.
If glob.glob() is good enough to replace your custom match_fname_pattern() you can save a few steps: pattern = os.path.join(sourcedir, "*.xml") files = glob.glob(pattern) files.sort(key=os.path.getmtime) for fn in files: read_and_concatenate(fn) Peter -- http://mail.python.org/mailman/listinfo/python-list