Save standard to a varible,standardvarible,import syste
文章由Byrx.net分享于2019-03-23 06:03:01
Save standard to a varible,standardvarible,import syste
import systemp = sys.stdout # save for restoringsys.stdout = open('log.txt', 'a') # redirects prints to fileprint x, y, x # print to filesys.stdout = tempprint a, b, c # print to original stdoutlog = open('log.txt', 'a') print >> log, x, y, z # print to a file-like objectprint a, b, c # print to original stdoutclass FileFaker: def write(self, string): # do something with the string passimport syssys.stdout = FileFaker()print someObjects # sends to the class write methodmyobj = FileFaker()print >> myobj, someObjects # does not reset sys.stdout
相关内容
- Comment in Python,commentinpython,## This is #
- elif demo,elif,num = input(
- Import a pre-defined module,importpre-defined,#//File: mod
- Module import,moduleimport,#File: perso
- Statement: pass,statementpass,# The pass s
- The type Function,thetypefunction,#returns the
- Small 'u' in front of the quote indicates that an Uni
- Assigning Multiple Values at Once,assigningmultiple,v = ('a', 'b
- While loop with counter-controlled repetition.,,total = 0
- SAX-Based Analysis of the Guacamole Recipe,sax-basedguacamole,impor
评论关闭