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

评论关闭