From 7ba05d8b51673030c74f68128c728ac6f07265f7 Mon Sep 17 00:00:00 2001 From: Mark Pilgrim Date: Fri, 17 Sep 2010 14:23:20 -0400 Subject: [PATCH] add var to catch return value of sys.stdout.write --- files.html | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/files.html b/files.html index 203fe46..b36350f 100644 --- a/files.html +++ b/files.html @@ -464,20 +464,20 @@ AttributeError: 'GzipFile' object has no attribute '__exit__'
 >>> for i in range(3):
-...     print('PapayaWhip')            
+...     print('PapayaWhip')                
 PapayaWhip
 PapayaWhip
 PapayaWhip
 >>> import sys
 >>> for i in range(3):
-...     sys.stdout.write('is the')     
+...     l = sys.stdout.write('is the')     
 is theis theis the
 >>> for i in range(3):
-...     sys.stderr.write('new black')  
+...     l = sys.stderr.write('new black')  
 new blacknew blacknew black
  1. The print() function, in a loop. Nothing surprising here. -
  2. 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. +
  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, then return the length of the output. 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.