import sys
temp = sys.stdout##先在ys.stdout = open("student.txt","w")之前定义才有效
?
sys.stdout.write(str(123))##实现print的功能,print实际上就是外设输入的数据写到了stdout流
sys.stdout = open("student.txt","w")##在创建一个文件并且写入
print("hello world")##在文件中写入的内容
print("word ppt excel")##由于定义了sys.stdout = open("student.txt","w")
##日后的print都会在指定的文件中输出
?
sys.stdout = temp##恢复默认的映射关系
print("nice")
?
?
原文:https://www.cnblogs.com/continue-student/p/14603515.html