Sys. stdout redirection

Source: Internet
Author: User

In general, the sys. stdout function is similar to the file output function fprintf in C ++.

Next, let's take a look at the Code:

>>> Import sys >>> temp = sys. stdout >>> sys. stdout = open ('e: \ pythonCode \ 1.txt ', 'w') # Note: dual \> print 1, 2, 3. the preceding three numbers are added to segment 1.txt> sys. stdout. close () # close, a bit similar to fclose ()

If we close and do not restore the output, let's see what the result will be:

>>> Print 1, 2, 3 Traceback (most recent call last): File "<pyshell #6>", line 1, in <module> print 1, 2, 3 ValueError: i/O operation on closed file

So we need to restore sys. stdout:

>>> Sys. stdout = temp >>> print 1, 2, 31 2 3

By the way, sys. stdout. flush () can immediately output data because sys. stdout is buffered by default.

Similar usage:

Sys. stdin, sys. stderr.

Related Article

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.