How to redirect python script cmd output to a file?

Viewed 9269

So I have a python script that outputs text to the console and I need that logged to a file instead, but the script is quite complex and I don't code python, so I would rather not alter it.

I want to do this

>python script.py arg1 arg2 ... argn > "somefile.txt"

But it won't work, and my guess is that python takes > and "somefile.txt" as arguments..

Can this be achieved and how?

3 Answers

Since the script is not yours, my guess is that it uses python logging module. If this is the case then you just need to reconfigure the logging module to use stdout. See https://stackoverflow.com/a/28194953/292787 for how to do it.

import logging
import sys
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
Related