On 20/02/07, Wolfgang Draxinger <[EMAIL PROTECTED]> wrote: > 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: [EMAIL PROTECTED], ICQ: 134682867
Are you running on windows? >>> i,o,e = os.popen3("dir c:\windows /OD /A-D /B") >>> [x.strip() for x in o.readlines() if 'a' in x ] Gives a list of filenames (but not directories) in date order, that match a pattern ('a' in x) >>> i,o,e = os.popen3("dir c:\windows /O-D /A-D /B") >>> [x.strip() for x in o.readlines() if 'a' in x] Gives the same list but in reverse chronological order. Something containing an approximation of my poor-writing style below would do the job. >>> i,o,e = os.popen3("dir c:\windows /OD /A-D /B") >>> [os.path.join(a_path, x.strip()) for x in o.readlines() if match_fname_pattern(x)] ['c:/windows/NeroDigital.ini', 'c:/windows/WindowsUpdate.log', 'c:/windows/setupapi.log', 'c:/windows/wiadebug.log', 'c:/windows/wiaservc.log' .................................... c:\> Dir /? will give you more help HTH :) -- http://mail.python.org/mailman/listinfo/python-list