아미(아름다운미소)

[python]print를 로그파일로 생성하기 본문

랭귀지/PYTHON

[python]print를 로그파일로 생성하기

유키공 2018. 11. 18. 11:30

python print를 로그파일로 생성하기

import sys
 class Tee(object):
     def __init__(self, *files):
         self.files = files
     def write(self, obj):
         for f in self.files:
             f.write(obj.encode('utf-8'))
 
 if __name__ == "__main__":
     f = open('logfile.txt', 'w')
     original = sys.stdout
     sys.stdout = Tee(sys.stdout, f)
     print "test~"  # This will go to stdout and the file out.txt
 
     #use the original
     sys.stdout = original
     print "Test log"  # Only on stdout
     f.close()

Comments