redirect read
import sys
name=input('please enter your name:')
print(name)
print('please enter your name:')
name=sys.stdin.readline() #redirect read
print (name)
f=open('e:/kangyujiao/test.txt','r')
sys.stdin=f
name=sys.stdin.readline() #redirect read
print (name)
class A:
def read(self):
return 'aaaaaaa'
sys.stdin=A()
name=sys.stdin.read() #redirect read
print (name)
redirect output
import sys
print('aaaaaaa')
old=sys.stdout
sys.stdout= open ( 'e:/kangyujiao/test.txt' , 'w' )
print ( 'bbbbbbbb' ) #Redirect the output to the specified file
sys.stdout=old
print ( 'ccccccc' )
f= open ( 'e:/kangyuujiao/test.txt' , 'a' )
print ( 'ddddddd' , file =f) #redirect to append to the specified file
class A:
def write( self ,s):
print ( s)
f=A()
print ( 'eeeeeee' , file =f) #Redirect output, as long as there is a write method
output exception
import sys
print ( 'aaaaaa' , file =sys.stderr) #The output is abnormal, the red text is displayed