Newline at EOF Removal

Mike Meyer mwm at mired.org
Mon Jan 9 14:58:39 EST 2006


"Alex N" <megahrtz at gte.net> writes:
> Peter gave me a good clue here
> w.write(f.read().rstrip('\n') + '\n')
> However the 2nd \n puts that empty line at the end of the file so I

No, it doesn't. Well, maybe it doesn't, depending on how your
applications treats newlines. Normally, a newline terminates a line,
so a file ending in "...some-alpha-text\n" ends in a complete
line. Python agrees with this, as file.readlines won't return a list
with a final empty string in it if fed a file whose last character is
a newline.

> tried it with just
> w.write(f.read().rstrip('\n'))
> and it works great!

This one ends in "...some-alpha-text". I'd say it ended in an
"incomplete" line, because the last line doesn't have a trailing
newline. My editor would agree - it warns me if I try and save a file
with an incomplete final line. 

>From what you've said, PHP apparently treats newlines as line
*separators*, not terminators. So a file that ends in a final newline
ends with a final blank line.

I suspect that part of your problem is trying to deal with the two
different views of what a newline does.

          <mike
-- 
Mike Meyer <mwm at mired.org>			http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.



More information about the Python-list mailing list