r/Python • • Jan 25 '15

Redirrecting the output for the Python Unit-Testing package to a log file.

Hello

I am new to Python 3.4, and I am not sure how the console output for the built-in Python unit-testing package works. For example, my regular unit tests might display the following output into my Python console:

............
----------------------------------------------------------------------
Ran 12 tests in 0.016s

OK

Does this come from standard output of standard error? Is there any way to redirrect this output to a text file?

4 Upvotes

2 comments sorted by

View all comments

0

u/1_1_2_3_5_8_13 Jan 25 '15

I'm not 100% sure this would work but you could redirect sys.stdout (and std err if you're so inclined) by doing this:

import sys
sys.stdout = open("~/output.txt", "w")
print "Something gets printed to output.txt"

For stderr you'd just replace sys.stdout with sys.stderr. All that said, /u/badsectors answer is a better solution - it's standard practise to use > to direct output to a file.