首页 > 编程 > Python > 正文

13_python_练习题——文件重定向

2019-11-06 08:03:41
字体:
来源:转载
供稿:网友

在程序中将输出内容重定向到一个文件中, 同时也能重定向回来

#!/usr/bin/python# -*- coding:UTF-8 -*-import sysdef testFuc():	PRint "It is a test"savedStdout = sys.stdout #保存标准输出流with open('out.txt', 'w+') as file:	sys.stdout = file #标准输出重定向至文件	print 'This message is for file!'	testFuc() sys.stdout = savedStdout #恢复标准输出流print 'This message is for screen!'testFuc()


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表