Python's sys.stdout, Sys.stdin redirection

Source: Internet
Author: User

Python's sys.stdout, Sys.stdin redirection

Transferred from: http://www.cnblogs.com/turtle-fly/p/3280519.html

This article environment: Python 2.7
Use print obj rather than print (obj)

Some background
sys.stdout and Printwhen 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_inputwhen 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 line
Hi=sys.stdin.readline () [: -1] #-1 to discard the ' \ n ' in input stream

Redirecting from console to fileOriginal 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_handler
print ' Hello '
# This hello can ' t is viewed on concole
# This hello are 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 End
sys.stdout=__console__

simultaneously redirect to console and fileWhat 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=a
print ' Hello '
OK, the above code is not working properly
Traceback (most recent): File
". \hello.py", line xx, in print ' Hello '
Attributeerror: ' str '
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 Sys
Class __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's sys.stdout, Sys.stdin redirection

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.