Python Standard output Sys.stdout redirection

Source: Internet
Author: User

Use print obj rather than print (obj)

Some background sys.stdout and print

When we print an object in Python and call print obj, the sys.stdout.write (obj+ ' \ n ') is actually called

Print Prints the content you want to the console, and then appends a line break

Print calls Sys.stdout's Write method

The following two lines are in fact equivalent:

Sys.stdout.write (' hello ' + ' \ n ') print ' Hello '
Sys.stdin and Raw_input

When we use Raw_input (' Input promption: '), the fact is to output the cue message first and then capture the input

The following two groups are in fact equivalent:

Hi=raw_input (' Hello? ') print ' Hello? ', #comma to stay in the same linehi=sys.stdin.readline () [: -1] #-1 to discard the ' \ n ' in input stream

Redirecting from console to file

Original Sys.stdout pointing to the console

If you assign a reference to a file object to Sys.stdout, then print calls the Write method of the file object

F_handler=open (' Out.log ', ' W ') sys.stdout=f_handlerprint ' Hello ' # this hello can ' t being viewed on concole# this hello was in File Out.log

Remember, if you still want to print something in the console, it's a good idea to save the original console object reference before you print it to the file and then restore the Sys.stdout

__console__=sys.stdout# redirection start# ... # redirection endsys.stdout=__console__
Simultaneously redirect to console and file

What if we want the printed content to output to the console on the one hand, and the output to a file to be saved as a log?

Leave the printed content in memory instead of a print to flush the buffer, so what happens when you put it in a string area?

A= ' sys.stdout=aprint ' Hello '

OK, the above code is not working properly

Traceback (most recent):  File ". \hello.py", line xx, in <module>    print ' Hello ' attributeerror: ' St R ' object has no attribute ' write '

The error is obvious, as highlighted above, when attempting to invoke Sys.stdout.write (), it is found that there is no write method

In addition, the reason why attribute error is not found here is because Python treats the object/class's function pointer record as a property of the object/class, but retains the entry address of the function.

In this case, we must implement a write method for the redirected object:

Import Sysclass __redirection__:        def __init__ (self):        self.buff= "        self.__console__=sys.stdout            def Write (self, output_stream):        self.buff+=output_stream            def to_console (self):        sys.stdout=self.__console __        Print Self.buff        def to_file (self, file_path):        f=open (File_path, ' W ')        sys.stdout=f        Print Self.buff        F.close ()        def flush (self):        self.buff= "            def Reset (self):        sys.stdout=self.__ console__        if __name__== "__main__":    # Redirection    r_obj=__redirection__ ()    sys.stdout=r_obj        # Get output stream    print ' hello '    print ' there '        # Redirect to console    R_obj.to_console ()        # Redirect to File    r_obj.to_file (' Out.log ')        # flush Buffer    R_obj.flush ()        # reset    r_ Obj.reset ()

Similarly, Sys.stderr, Sys.stdin can be redirected to more than one address, extrapolate of things to do their own hands-on

Python standard output sys.stdout redirection

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.