You are here: > Dive Into Python > Scripts and Streams > Standard input, output, and error | << >> | ||||
Dive Into PythonPython from novice to pro |
UNIX users are already familiar with the concept of standard input, standard output, and standard error. This section is for the rest of you.
Standard output and standard error (commonly abbreviated stdout and stderr) are pipes that are built into every UNIX system. When you print something, it goes to the stdout pipe; when your program crashes and prints out debugging information (like a traceback in Python), it goes to the stderr pipe. Both of these pipes are ordinarily just connected to the terminal window where you are working, so when a program prints, you see the output, and when a program crashes, you see the debugging information. (If you're working on a system with a window-based Python IDE, stdout and stderr default to your Interactive Window.)
>>> for i in range(3): ... print 'Dive in'Dive in Dive in Dive in >>> import sys >>> for i in range(3): ... sys.stdout.write('Dive in')
Dive inDive inDive in >>> for i in range(3): ... sys.stderr.write('Dive in')
Dive inDive inDive in
stdout and stderr are both file-like objects, like the ones you discussed in , but they are both write-only. They have no read method, only write. Still, they are file-like objects, and you can assign any other file- or file-like object to them to redirect their output.
[you@localhost kgp]$ python stdout.py Dive in [you@localhost kgp]$ cat out.log This message will be logged instead of displayed
(On Windows, you can use type instead of cat to display the contents of a file.)
If you have not already done so, you can download this and other examples used in this book.
#stdout.py import sys print 'Dive in'saveout = sys.stdout
fsock = open('out.log', 'w')
sys.stdout = fsock
print 'This message will be logged instead of displayed'
sys.stdout = saveout
fsock.close()
![]()
Redirecting stderr works exactly the same way, using sys.stderr instead of sys.stdout.
[you@localhost kgp]$ python stderr.py [you@localhost kgp]$ cat error.log Traceback (most recent line last): File "stderr.py", line 5, in ? raise Exception, 'this error will be logged' Exception: this error will be logged
If you have not already done so, you can download this and other examples used in this book.
#stderr.py import sys fsock = open('error.log', 'w')sys.stderr = fsock
raise Exception, 'this error will be logged'
![]()
![]()
Since it is so common to write error messages to standard error, there is a shorthand syntax that can be used instead of going through the hassle of redirecting it outright.
>>> print 'entering function' entering function >>> import sys >>> print >> sys.stderr, 'entering function'entering function
Standard input, on the other hand, is a read-only file object, and it represents the data flowing into the program from some previous program. This will likely not make much sense to classic Mac OS users, or even Windows users unless you were ever fluent on the MS-DOS command line. The way it works is that you can construct a chain of commands in a single line, so that one program's output becomes the input for the next program in the chain. The first program simply outputs to standard output (without doing any special redirecting itself, just doing normal print statements or whatever), and the next program reads from standard input, and the operating system takes care of connecting one program's output to the next program's input.
[you@localhost kgp]$ python kgp.py -g binary.xml01100111 [you@localhost kgp]$ cat binary.xml
<?xml version="1.0"?> <!DOCTYPE grammar PUBLIC "-//diveintopython.org//DTD Kant Generator Pro v1.0//EN" "kgp.dtd"> <grammar> <ref id="bit"> <p>0</p> <p>1</p> </ref> <ref id="byte"> <p><xref id="bit"/><xref id="bit"/><xref id="bit"/><xref id="bit"/>\ <xref id="bit"/><xref id="bit"/><xref id="bit"/><xref id="bit"/></p> </ref> </grammar> [you@localhost kgp]$ cat binary.xml | python kgp.py -g -
![]()
10110001
So how does the script know to read from standard input when the grammar file is -? It's not magic; it's just code.
def openAnything(source): if source == "-":import sys return sys.stdin # try to open with urllib (if source is http, ftp, or file URL) import urllib try: [... snip ...]
<< Scripts and Streams | | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | | Caching node lookups >> |