+2 votes
in Programming Languages by (15.0k points)
I have to pipe the output of a Python program to a file because of some restrictions on the machine where I am running my code. There are several print statements in the program. I want to see those print statements on my terminal. How can I do that?

1 Answer

+1 vote
by (25.0k points)

If you are using Python 2, do the followings:

Step 1:

import sys

and write sys.stdout.flush() after every print statement in your program.

Step 2:

On the command line, run "tail -f out.log" where out.log is the name of the piped file. If you have used a different name, replace out.log with that name.

If you are using Python 3.3+, do the followings:

Step 1:

In all print statements, add flush=True as a parameter. E.g. print ('Hello', flush=True)

Step 2:

On the command line, run "tail -f out.log" where out.log is the name of the piped file. If you have used a different name, replace out.log with that name.

...