Python takes standard output (sys.stdout) as an example to see Python's standard input, standard error output

Source: Internet
Author: User

Read a blog, very good. Http://www.cnblogs.com/turtle-fly/p/3280519.html

The standard output (sys.stdout) corresponds to print (printing), and the standard input (Sys.stdin) corresponds to the input (receive input) operation, and the standard error output and standard output are similar to print (printing).

Python's most basic operation-printing:

1

The effect is to write 1 in the console (command line) to let you see.

In fact, his operation can be understood as: the console (command line) as a board, through the Sys.stdout = console designated to the console board to write things (console is the default, that is, you do not change where to write, it will be written by default) , in print 1, just tell Python that I'm going to write 1, and then Python will go to the board specified by Sys.stdout, which is the console (command line) that writes 1.

(The standard error output is the same process, and you'll find that when the program goes wrong, the error message is printed in the console.) )

In fact, as long as an object has the Write method, it can be used as a "board", tell Sys.stdout where to write.

Speaking of the Write method, the first thing you might think of is file manipulation.

F=open ('log.txt','w')

To declare a file object F as above, this file object has the Write method, which can be used as the standard output and categorization malleability the error output of the board.

F=open ('log.txt','w')__console__ = Sys.stdout    # make a backup of the default "board"-command line so that it can be changed back  = fprint 1

__console__
Print 2

The above operation, by sys.stdout = f Specifies that the board is changed to F when printing. So when using print, it is no longer to print 1 on the command line, but in the Log.txt file.

And then changed the board to the command line, and then print 2 prints 2 to the command line.

Python takes standard output (sys.stdout) as an example to see Python's standard input, standard error output

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.