7

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?

Garin GG
  • 71
  • 1
  • 1
  • 3
  • 1
    Your guess is wrong. In what way doesn't it work? What do you see? – immortal Aug 04 '17 at 06:29
  • I see the output in the console, and there is no file created. – Garin GG Aug 04 '17 at 06:31
  • What shell? And how does your script prints output? – immortal Aug 04 '17 at 06:33
  • 1
    Are you certain its not outputting to STDERR? – cfeduke Aug 04 '17 at 06:35
  • 1
    Did you add the CMD tag because you're running Python from the Windows CMD shell? In that case, if `python script.py ... > somefile.txt 2>&1` doesn't work, then the script is writing directly to the console instead of stdout or stderr. – Eryk Sun Aug 04 '17 at 06:54
  • Yes, that worked, post as answer please... – Garin GG Aug 04 '17 at 07:09
  • 1
    Possible duplicate of [How to execute a python script and write output to txt file?](https://stackoverflow.com/questions/21963270/how-to-execute-a-python-script-and-write-output-to-txt-file) – Clock Slave Aug 04 '17 at 08:17
  • Have you tried this: https://stackoverflow.com/questions/21963270/how-to-execute-a-python-script-and-write-output-to-txt-file – Clock Slave Aug 04 '17 at 08:17
  • I am facing the same problem like @GarinGG. An empty file is created, but not written to. How did you solve it in the end? Strangely that happen when I added a simple if statement in the code. But now also the old code does not work anymore, so the problem is somewhere else. You can find the code here: https://github.com/gesiscss/Improved-Gender-Identifier/blob/master/retrieve_data/scrapeImages.py. – PeterHeuz Aug 26 '20 at 09:34
  • @ErykSun Do you have a solution ? – PeterHeuz Aug 26 '20 at 11:34

4 Answers4

7

$ (python script.py one two) > test.txt

Or if you want to see the output and also write it to a file:

$ python script.py one two | tee test.txt

If this still isn't writing to the file, try redirecting STDERR:

$ python script.py one two 2>&1 | tee test.txt

cfeduke
  • 23,100
  • 10
  • 61
  • 65
  • 1
    Tried the one with the brackets and the file gets created but the output isn't redirected and the file remains empty. Sorry but I don't understand your syntax for the rest.. By '$' do you imply linux bash? Because I'm talking about windows command prompt – Garin GG Aug 04 '17 at 07:04
  • Yeah, got "'tee' is not recognized as an internal or external command..." – Garin GG Aug 04 '17 at 07:06
  • 1
    @GarinGG, Windows isn't distributed with a `tee` command. You can install one, but you didn't ask about that anyway. Just redirect stdout via `>somefile.txt` and redirect stderr to stdout (i.e. the same file) via `2>&1`. – Eryk Sun Aug 04 '17 at 08:01
  • Yeah sorry, used to writing my bash examples starting with a `$` and didn't even think about Windows. If you are using Cygwin it may include `tee` (dunno, its been years). – cfeduke Aug 05 '17 at 15:16
3

Add these lines of code to the beginning of the python file.

import sys
sys.stdout = open('somefile.txt', 'w')

This is setting the sys.stdout to a file object of your choice.

2

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)
Stanislav
  • 4,389
  • 2
  • 33
  • 35
0

Use:

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

emcee
  • 1
  • 2