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
print() statement, in a loop. Nothing surprising here.
+print() function, in a loop. Nothing surprising here.
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.
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.
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.
-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.
-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.
+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.
+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.