reading stdinput

Sheila King sheila at spamcop.net
Sun Mar 18 00:43:39 EST 2001


On Sun, 18 Mar 2001 11:11:00 +0930 (CST), Michael Hall <admin at mulga.com.au>
wrote in comp.lang.python in article
<mailman.984885323.10683.python-list at python.org>:

:How do I read standard input into a Python script? I'm trying to do the
:same as 'read' in a shell script to create a variable. I've played about
:with various combinations of 'sys.stdin.read()' and such but no go.

Here are two lines from a script I use for filtering my incoming e-mail:

	origheaders=rfc822.Message(sys.stdin, 0)
	raw_body = sys.stdin.read()

You have to import the sys module to use that.

sys.stdin is a file object. It behaves like a file pointer (fp).

Later in my script, I have the following line:

	body = string.join(raw_body, "")

This takes the raw_body, and joins it into one single string, instead of a
list of strings.

:If there is a better list for newbie questions like this, please let me
:know.

So far as I can tell (I'm a newbie, too), this list/newsgroup is fine. But,
you could also try the Python Tutor mailing list, as well. Info is here:

http://mail.python.org/mailman/listinfo/tutor

--
Sheila King
http://www.thinkspot.net/sheila/
http://www.k12groups.org/




More information about the Python-list mailing list