stdin -> stdout

Steven Bethard steven.bethard at gmail.com
Fri Aug 19 18:08:53 CEST 2005


gry at ll.mit.edu wrote:
> import sys
> for l in sys.stdin:
>         sys.stdout.write(l)

This is fine if you don't need the reads and writes of lines to run in 
lockstep.  File iterators read into a buffer, so you'll probably read 
4096 bytes from stdin before you ever write a line to stdout.  If that's 
okay, this is a good solution.  OTOH, if you want the reads and writes 
to run in lockstep, you should probably use this idiom:

import sys
for line in iter(sys.stdin.readline, ''):
     sys.stdout.write(line)

STeVe

P.S. You may also be able to get your version working using the -u 
(unbuffered) option to Python, but I couldn't.



More information about the Python-list mailing list