idiomatic analogue of Perl's: while (<>) { ... }

Sahil Tandon sahil at
Thu Sep 1 06:56:50 CEST 2011

I've been tasked with converting some programs from Perl -> Python, and
am (as will soon be obvious) new to the language.  A few archive/google
searches were inconclusive on a consensus approach, which is OK, but I
just wonder if there is a more Python-esque way to do the following in
Python 2.7.1:

# unbuffer STDOUT
sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)

# process input, line-by-line, and print responses after parsing input 
while 1:
  rval = parse(raw_input())
  if rval == None:

This works, but while reading the documentation, I thought of using 'for
line in fileinput.input()' in lieu of 'while 1:' construct.  This does
not work when debugging the program on the command line -- the script
appears to just hang no matter what is typed into STDIN.  I believe this
is because of some internal buffering when using fileinput.  Is there a
recommended way to disable such buffering?  Am I taking a totally wrong

Feel free to just link me to previous discussions on the topic(s) if I
have missed them.  Please be gentle with your cluebats. :-)

Sahil Tandon <sahil at>

More information about the Python-list mailing list