Python vs. Perl, which is better to learn?
Alex Martelli
aleax at aleax.it
Thu May 2 05:16:55 EDT 2002
brueckd at tbye.com wrote:
...
>> and figure out how to get data in
>
> data = sys.stdin.read() # Time: 2 seconds
>
> or
>
> while 1:
> line = sys.stdin.readline()
> if not line: break
>
> # Time: 6 seconds
Actually, I think the "one obvious way to do it" is
(adding fileinput to the modules you import, and):
for line in fileinput.input():
whatever(line)
This gives you stdin if your script is called w/o
arguments, or else the files with whose names your program
is called -- basically like Perl's "while(<>)" (one
enhancement to fileinput I'd like is a switch to have
it implicitly call glob.glob on arguments that have
wildcards when running on Windows, because I do find
myself all the time wanting to use "python x.py *.foo"
indifferently on Windows or Unix-like systems -- but
that's another issue. I guess).
Alex
More information about the Python-list
mailing list