Issue
I am making a server with Python 2. For that I use the module CGIHTTPServer and I want to redirect only the outputs of that module to a file. I tried already CGIHTTPServer.sys.stdout = open("file.log", "w")
, CGIHTTPServer.SimpleHTTPServer.sys,stdout = open("file.log", "w")
, but both had simply no effect. Is that possible at all, and if yes, how ?
Solution
In a module you want to redirect everything to stdout.
import sys
stdout_sav = sys.stdout
fout = open('file.log', 'w')
sys.stdout = fout
# since now, whatever is printed to standard output goes to file
(...)
# at the end restore standard output
sys.stdout = stdout_sav
fout.close()
Answered By - tansy
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.