How do I iterate over all lines of files passed on the command line?

Tg. picture Tg. · Apr 3, 2009 · Viewed 17.2k times · Source

I usually do this in Perl:

whatever.pl

while(<>) {
    #do whatever;
}

then cat foo.txt | whatever.pl

Now, I want to do this in Python. I tried sys.stdin but I have no idea how to do as I have done in Perl. How can I read the input?

Answer

Don Werve picture Don Werve · Apr 3, 2009

Try this:

import fileinput
for line in fileinput.input():
    process(line)