Created
January 4, 2019 02:53
-
-
Save liziwl/86b3d70b6675473b999c5e38b305ffc6 to your computer and use it in GitHub Desktop.
python 重定向
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
f_handler=open('out.log', 'w') | |
sys.stdout=f_handler | |
print('hello') | |
# this hello can't be viewed on concole | |
# this hello is in file out.log | |
f_handler.close() | |
import sys | |
f=open('a.txt','w') | |
old=sys.stdout #将当前系统输出储存到一个临时变量中 | |
sys.stdout=f #输出重定向到文件 | |
print 'Hello weird' #测试一个打印输出 | |
sys.stdout=old #还原原系统输出 | |
f.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment