Read from File, or STDIN

The fileinput module may do what you want – assuming the non-option arguments are in args then:

import fileinput
for line in fileinput.input(args):
    print line

If args is empty then fileinput.input() will read from stdin; otherwise it reads from each file in turn, in a similar manner to Perl’s while(<>).

Leave a Comment