python - Redirecting stdout, stderror to file while stderr still prints to screen -



python - Redirecting stdout, stderror to file while stderr still prints to screen -

i stdout , stderr redirected same file, while stderr still writes screen. what's pythonic way this?

i'm assuming want redirect current script's stdout , stderr, not subprocess you're running.

this doesn't sound pythonic thing in first place, if need to, pythonic solution be:

redirect stdout file. redirect stderr custom file-like object writes file , writes real stderr.

something this:

class tee(object): def __init__(self, f1, f2): self.f1, self.f2 = f1, f2 def write(self, msg): self.f1.write(msg) self.f2.write(msg) outfile = open('outfile', 'w') sys.stdout = outfile sys.stderr = tee(sys.stderr, outfile)

python redirect stderr

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -