[Patches] fileinput.py argument handling (and suggestion)

Guido van Rossum guido@python.org
Sun, 09 Apr 2000 18:18:10 -0400

> Agreed.  "chop" is a special case where you have to detect different line
> termination cases on a platform-specific basis.  Plain old line stripping is 
> handled just fine by existing functions.

Not really.  Chop is special because it doesn't exist as a built-in.
What we really want is Perl's chomp, which can be coded in Python as

def chomp(s):
    if s[-1:] == '\n': return s[:-1]
    else: return s

Perl's chop() is like s[:-1], and steals the last character of the
file if it is not a newline.

The CR-LF issue is taken care of at a different level:

> Speaking of chop, If I am on a platform X box (X in Unix, Windows, Mac, Be)
> and try to fileinput.input(chop=1) a file from a different box, will it do
> the right thing (e.g. recognize ^M as a line termination character on a Unix 
> or Windows machine)?

Currently, it won't.  A good change for Python would be for files
opened for reading in text mode (on all platforms!) to use Java's
algorithm (line terminators are either CR, LF or CRLF) but to
translate all three kinds of terminator into \n, so that Python code
can be written to look for \n everywhere.  (This is the intention of
the current text mode too, but it fails when a file uses another
platform's convention.)

--Guido van Rossum (home page: http://www.python.org/~guido/)