How do I redirect stdout to an arbitrary file in Python?
When a long-running Python script (e.g, web application) is started from within the ssh session and backgoun
If you want to do the redirection within the Python script, setting sys.stdout to a file object does the trick:
sys.stdout
import sys sys.stdout = open('file', 'w') print('test')
A far more common method is to use shell redirection when executing (same on Windows and Linux):
$ python foo.py > file