From 61d5811c04172070d684de4302a55644501e2062 Mon Sep 17 00:00:00 2001 From: Mark Pilgrim Date: Sat, 26 Sep 2009 00:09:22 -0400 Subject: [PATCH] "standard out" --> "standard output" --- files.html | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/files.html b/files.html index a0dc2b3..cf482d2 100644 --- a/files.html +++ b/files.html @@ -451,7 +451,7 @@ PapayaWhip ... sys.stderr.write('new black') new blacknew blacknew black
    -
  1. The print() statement, in a loop. Nothing surprising here. +
  2. The print() function, in a loop. Nothing surprising here.
  3. stdout is defined in the sys module, and it is a stream object. Calling its write() function will print out whatever string you give it. In fact, this is what the print function really does; it adds a carriage return to the end of the string you’re printing, and calls sys.stdout.write.
  4. In the simplest case, sys.stdout and sys.stderr send their output to the same place: the Python IDE (if you’re in one), or the terminal (if you’re running Python from the command line). Like standard output, standard error does not add carriage returns for you. If you want carriage returns, you’ll need to write carriage return characters.
@@ -555,8 +555,8 @@ print('C')
  1. This will print to the IDE “Interactive Window” (or the terminal, if running the script from the command line).
  2. This with statement takes a comma-separated list of contexts. The comma-separated list acts like a series of nested with blocks. The first context listed is the “outer” block; the last one listed is the “inner” block. The first context opens a file; the second context redirects sys.stdout to the stream object that was created in the first context. -
  3. Because this print() statement is executed with the contexts created by the with statement, it will not print to the screen; it will write to the file out.log. -
  4. The with code block is over. Python has told each context manager to do whatever it is they do upon exiting a context. The context managers form a last-in-first-out stack. Upon exiting, the second context changed sys.stdout back to its original value, then the first context closed the file named out.log. Since standard out has been restored to its original value, calling the print() function will once again print to the screen. +
  5. Because this print() function is executed with the context created by the with statement, it will not print to the screen; it will write to the file out.log. +
  6. The with code block is over. Python has told each context manager to do whatever it is they do upon exiting a context. The context managers form a last-in-first-out stack. Upon exiting, the second context changed sys.stdout back to its original value, then the first context closed the file named out.log. Since standard output has been restored to its original value, calling the print() function will once again print to the screen.

Redirecting standard error works exactly the same way, using sys.stderr instead of sys.stdout.