Combining several text files

Scott David Daniels Scott.Daniels at Acm.Org
Mon Feb 2 13:30:42 EST 2009


Eric wrote:
> I have to parse several log files....
> Then I can do one of two things. First I could combine them so that
> the resulting file ends up with the oldest on top and newest on the
> bottom. Otherwise, I could just iterate over the multiple files within
> my parser.
> 
> I don't need working code (that makes things too easy), just clear
> suggestions to a Python newcomer to speed me on my way.

Look into the "glob" module to get your name list, and sort the
resulting list appropriately, then you can loop over that list.
Then you can use an iterator (similar to below) to produce lines:

     def source_lines(chronological_filenames):
         for name in chronological_filenames:
             with open(name) as source:
                  for line in source:
                      yield line

Note this uses the with statement to do file closing, so if you are
using an older Python, you may need to write it differently.

You can use this as a source of lines for your parser.

--Scott David Daniels
Scott.Daniels at Acm.Org



More information about the Python-list mailing list