Inserting/Deleting newline(s) in very large text files

Thomas Jollans thomas at jollybox.de
Thu Aug 12 15:25:59 EDT 2010


On Thursday 12 August 2010, it occurred to Dlanor Slegov to exclaim:
> Hi,
> 
> I am dealing with very large text files (a few million lines) and would
> like to check and modify them according to a well defined format. The
> format requires ONLY ONE NEWLINE (followed by some sort of text) on top of
> the file and NO NEWLINE in the very end. The input files can be very
> diverse, such as one file may have 2 newlines on top and none in the end
> or other may not have a newline on top and 5 in the end.
> 
> 
> The aim is to check these files & introduce JUST ONE NEWLINE in the TOP and
> NO NEWLINE in the BOTTOM.
> 
> Any pointers will be appreciated.

If the files are that long, you might want to avoid storing the whole file in 
memory at the same time.

Have you already tried writing the program? If not, go write the program! (and 
then come back, possibly, if you have questions)


PS: What a strange format that must be. Everybody agrees that text files 
should end with a line feed, because some text editors might mess up if they 
don't.



More information about the Python-list mailing list