newbie question: stdin, stdout, stderr?

Skip Montanaro skip at mojam.com
Thu Jan 27 12:14:59 EST 2000


    Warren> How do you first USE, and second REDIRECT stdin, stdout, and
    Warren> stderr from within a Python program?

stdin, stdout and stderr are available as file objects in the sys module:

    >>> import sys
    >>> sys.stdin
    <open file '<stdin>', mode 'r' at 8111298>

You can redirect them by assigning an open file object to them:

    >>> sys.stderr = open("/tmp/errors", "a")

then when you're finished, reassign them using the __ versions:

    >>> sys.stderr = sys.__stderr__ 

Skip Montanaro | http://www.mojam.com/
skip at mojam.com | http://www.musi-cal.com/
847-971-7098
"Languages that change by catering to the tastes of non-users tend
not to do so well." - Doug Landauer




More information about the Python-list mailing list