Skip to content

Instantly share code, notes, and snippets.

@liziwl
Created January 4, 2019 02:53
Show Gist options
  • Save liziwl/86b3d70b6675473b999c5e38b305ffc6 to your computer and use it in GitHub Desktop.
Save liziwl/86b3d70b6675473b999c5e38b305ffc6 to your computer and use it in GitHub Desktop.
python 重定向
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