Text processing and file creation

kyosohma at gmail.com kyosohma at gmail.com
Wed Sep 5 12:23:12 EDT 2007


On Sep 5, 11:13 am, "malibus... at gmail.com" <malibus... at gmail.com>
wrote:
> I have a text source file of about 20.000 lines.>From this file, I like to write the first 5 lines to a new file. Close
>
> that file, grab the next 5 lines write these to a new file... grabbing
> 5 lines and creating new files until processing of all 20.000 lines is
> done.
> Is there an efficient way to do this in Python?
> In advance, thanks for your help.

I would use a counter in a for loop using the readline method to
iterate over the 20,000 line file. Reset the counter every 5 lines/
iterations and close the file. To name files with unique names, use
the time module. Something like this:

x = 'filename-%s.txt' % time.time()

Have fun!

Mike




More information about the Python-list mailing list