Python print Sys.stdout Sys.stderr

Source: Internet
Author: User
Tags one more line

Reference documents

Python redirection standard input, standard output, and standard error

http://blog.csdn.net/lanbing510/article/details/8487997

Python redirects Sys.stdin, sys.stdout, and Sys.stderr

Http://www.cnblogs.com/guyuyuan/p/6885448.html

1.print

Print obj actually calls sys.stdout.write (obj+ ' \ n '), paying attention to one more line break

1a. Print differences on Python 2.X and Python 3

The print function for Python 3 should be two more option parameters: End, file

    • End can control whether print ends with a newline, no wrapping, or other characters.
#不换行print ("xx", end= ")
    • File can control whether the output is sys.stdout or Sys.stderr
Print ("XXX", Sys.stderr)

2.sys.stdout and Sys.stderr

2a. Definition

‘‘‘

standard output and standard errors (usually abbreviated as STDOUT and stderr) are pipelinesbuilt within each UNIX system.

When you print something, the result is output to the stdout pipeline , and when your program crashes and prints out debugging information (like error tracking in Python), the result is output to the stderr pipeline .

usually these two pipelines are only associated with the terminal window you are working on , so when a program prints out the output, you can see the output, and when a program crashes, you can see the debug information. (If you are working on a Windows-based Python IDE system, stdout and stderr default to "interactive Windows.") )

‘‘‘

2b. Using

‘‘‘

both stdout and stderr are class file objects , just as we discussed in extracting input sources, but they are all write-only. They have no Read method, only write.

However, they are indeed class file objects, and you can assign arbitrary file objects or class file objects to them to redirect the output.

‘‘‘

2c. Buffering

There are buffers in the sys.stdout, and there are two ways to resolve the buffer:

    • Print () or sys.stdout.write () plus Sys.stdout.flush ()
    • Add-u parameter when executing python script, i.e. python-u xxx.py

3. Special case CGI and redirected output

Through the above, the original stdout and stderr are all pointing to the console

3a. The stdout of CGI

If you use Python to write CGI scripts where stdout is the page that points to the output, stderr still points to the console

So the direct use of print xxx,xxx is directly generated into the page, if you want to debug log, there are two ways:

    • Output by stderr;
    • Temporarily modify the stdout to point to the console, which is the redirected output

3b. REDIRECT Output

    • Redirect output to a file
sys.stdout=f_handler print ' Hello '
    • REDIRECT output to standard output
#!/usr/bin/env pythonimport cgi# under CGI script, sys.stdout output to Web page web_out = sys.stdout    # Save Output to Web page status Sys.stdout = sys.stderr< c1/># change the standard output to console Sys.stdout.write (' sys.stdout console ')    # sys.stdout Print information to console print ("Print-to-console")    # Print information to console sys.stdout = web_out    # change standard output to Web page sys.stdout.write (' sys.stdout-Web ')    # Print information via sys.stdout to Web page print ("Print-web")    

  

Talk clean, End ~

Python print Sys.stdout 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.