[Tutor] file I/O

Michael P. Reilly arcege@speakeasy.net
Wed, 11 Jul 2001 16:56:12 -0400 (EDT)


Massey, Craig wrote
> 
> Is there a "standard" way of removing the newline, like chomp in Perl?

Mostly, I use string.strip or string.rstrip.  But that removes all
whitespace, not just line terminators (carriage returns and newlines).
When reading in text format (not 'rb'), then the terminator gets converted
to '\n'.  So you can safely remove that from the end.

In binary, since each system is different and since you could get a
UNIX file on a Mac, the line terminator could be different.  What is
the terminator, the carriage return (Mac standard) or the newline (UNIX
standard of the file)?  That is the problem with making one function
(chomp) do to that.

But readline simplifies this as I said above.  Just using
f.readline()[:-1] is good enough.

  -Arcege


-- 
+----------------------------------+-----------------------------------+
| Michael P. Reilly                | arcege@speakeasy.net              |