fileinput (was RE: "sins" (aka, acknowledged language problems))

Alex Martelli Alex.Martelli at think3.com
Fri Dec 17 09:14:15 EST 1999


Alexander Williams writes:

> >import fileinput
> >for line in fileinput.input():
> 
> The funny thing is that if Python had been /less/ intuitive, he'd have
> stumbled over this class in the documentation in the course of digging
> up file management.  In fact, if he'd decided to just keep fiddling
> with it for another half hour out of sheer bloody-mindedness, he would
> have found it.
> 
Assuming the "he" you mean is "me", I had indeed "stumbled
over" the fileinput module, but it had struck me as an elegant
approach to implement the typical "while(<>)" Perl idiom (with
clearer syntax and more generality, of course, but that's par
for the course:-); hardly appropriate for a function meant to
copy-with-translation one arbitrary file onto another one, with
stdin and stdout being just the defaults.  Looking again at it
I saw it had a bit more depth and generality, in that a FileInput
object (or something polymorphic with it regarding iteration)
might be about equivalent, as an argument to the function, to
a file object (or something polymorphic with it regarding the
readline method) -- perhaps better -- and surely allow the
superior loop syntax I hanker for.

Reading through the library docs doesn't guarantee one will
realize the exact breadth and applicability of each module,
of course -- that's what makes one a newbie as opposed to
an old hand, innit?-)


Alex





More information about the Python-list mailing list