[Python-Dev] RE: fileinput.py

Thomas Wouters thomas@xs4all.net
Sat, 6 Jan 2001 00:18:58 +0100


On Fri, Jan 05, 2001 at 04:01:53PM -0600, Ka-Ping Yee wrote:

>     while (<>) {
>         ...
>     }

> is simple, quickly learned, frequently used, and instantly recognizable.

>     import sys
>     if len(sys.argv) > 1:
>         file = open(sys.argv[1])
>     else:
>         file = sys.stdin
>     while 1:
>         line = file.readline()
>         if not line:
>             break
>         ...

... Except that it can take more than one filename, and will do the one
after another, and that it takes "-" as a filename for stdin. Doing it in a
script is not dead simple, unless you open up all files at once (which can
be harmful, and Perl, for one, doesn't do) or you do most of the work
fileinput does. That is why I use fileinput (and while-diamond) -- I might
not need it now, but when I do need it, it already works :)

-- 
Thomas Wouters <thomas@xs4all.net>

Hi! I'm a .signature virus! copy me into your .signature file to help me spread!