I usually do this in Perl:
whatever.pl
while(<>) {
#do whatever;
}
then cat foo.txt | whatever.pl
Now, I
Try this:
import fileinput
for line in fileinput.input():
process(line)
I hate to beat a dead horse, but may I suggest using a pure function?
import sys
def main(stdin):
for line in stdin:
print("You said: " + line.strip())
if __name__ == "__main__":
main(sys.stdin)
This approach is nice because main is dependent purely on its input and you can unit test it with anything that obeys the line-delimited input stream paradigm.
Something like this:
import sys
for line in sys.stdin:
# whatever
import sys
def main():
for line in sys.stdin:
print line
if __name__=='__main__':
sys.exit(main())
import sys
for line in sys.stdin:
# do stuff w/line