阅读背景:

打印print的另一种方式sys.stdout

来源:互联网 
>>> import sys
>>> tmp = sys.stdout
>>> sys.stdout=open('log.txt','a')
>>> print('cisco')
>>> print(1,2,3)
>>> sys.stdout.close()
>>> sys.stdout=tmp
>>> print('haha')
haha
>>> print(open('log.txt','rb').read())
b'welcome to haha\r\ncisco\r\n1 2 3\r\n'
>>> print(open('log.txt','r').read())
welcome to haha
cisco
1 2 3

>>> 
>>> import sys
>>> tmp = sys.stdout
>>> sys



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: