redirect prints to log file

前端 未结 8 1669
萌比男神i
萌比男神i 2020-12-02 09:05

Okay. I have completed my first python program.It has around 1000 lines of code. During development I placed plenty of print statements before running a command

8条回答
  •  余生分开走
    2020-12-02 09:51

    Python lets you capture and assign sys.stdout - as mentioned - to do this:

    import sys
    old_stdout = sys.stdout
    
    log_file = open("message.log","w")
    
    sys.stdout = log_file
    
    print "this will be written to message.log"
    
    sys.stdout = old_stdout
    
    log_file.close()
    

提交回复
热议问题