Readling all input from stdin

Bengt Richter bokr at oz.net
Mon Jul 28 22:00:39 CEST 2003


On Mon, 28 Jul 2003 13:25:33 +0200, Svenne Krap <usenet2003 at krap.dk> wrote:

>Hi.
>
>I am writing a small script, that is called from an external program 
>(the qmail mailserver), I need to capture all input from stdin before 
>continueing the script ..
>
>I looked for sys.stdin.readlines() but that is not present. I found 
>sys.stdin.readline(), but how do I loop through it (or rather how do I 
>know, when I am finished with looping ?)
>
>Thanks in advance
>
====< stdinlines.py >=======================================================
import sys
data = sys.stdin.read()
for line in data.splitlines(True): # keeps \n ends. (False) or () would not
    print repr(line) # to see all chars represented
============================================================================

(I have a cat on my NT that copies stdin to stdout in text mode until it sees ^Z as EOF):

[13:00] C:\pywk\clp>cat | python stdinlines.py
trimmed
   three spaces to left
three spaces to right
   three spaces both sides
next line is empty

last line with no EOL^Z
'trimmed\n'
'   three spaces to left\n'
'three spaces to right   \n'
'   three spaces both sides   \n'
'next line is empty\n'
'\n'
'last line with no EOL'


Regards,
Bengt Richter




More information about the Python-list mailing list