Readling all input from stdin

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

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

>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
====< >=======================================================
import sys
data =
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
   three spaces to left
three spaces to right
   three spaces both sides
next line is empty

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

Bengt Richter

More information about the Python-list mailing list