2

I can run my python scripts on the terminal and get the print results on the stdout e.g.

python myprog.py

or simply redirect it to a file:

python myprog.py > out.txt

My question is how could I do both solutions at the same time. My linux experience will tell me something like:

python myprog.py |& tee out.txt

This is not having the behaviour I expected, print on the fly and not all at once when the program ends.

So what I wanted (preferred without changing python code) is the same behavior as python myprog.py (print on the fly) but also redirecting output to a file.

What is the simplest way to accomplish this?

Alg_D
  • 2,242
  • 6
  • 31
  • 63

2 Answers2

0

You want to use 'tee'. stdbuf -oL python mycode.py | tee out.txt

Oli
  • 365
  • 2
  • 6
  • 1
    I tried that as mentioned. this only puts the results on the stdout when the script finishes, I'd like to have stdout updated on the fly! – Alg_D Mar 11 '16 at 13:40
  • Try placing stdbuf -oL in front of python like in my update. See the man page for explanation http://linux.die.net/man/1/stdbuf – Oli Mar 11 '16 at 13:57
0

Late to the party (my last answer was bad), but you could look into the logging library. See here for a more ample documentation, but the gist of it is to use two outputs for the logging library. A similar question can be found here.

Community
  • 1
  • 1
alpha1554
  • 605
  • 5
  • 15