Search code examples
pythontee

a python print statement not shown when run through pipeline and tee command


I'm using simple function w1(str) to sometimes make the program stop and wait for a key. The code below works.

file : test.py

#!/bin/env python

def w1(str):
    print (str)
    wait = raw_input()
    return

if __name__ == '__main__':
    print('started..')
    w1('press a key')
    print('exiting..')

How it runs (ok) :

ckim@stph45:~/test] test.py
started..
press a key

exiting..

But when I run test.py | tee log, it doesn't show anything and if I press a key it finishes not printing the message and waiting for the key. Actually, teh problem is the print(str) is not displayed on the screen when run through tee command and it was waiting for my key. How can I make this print(str) shown even when I run it with |tee ?

EDIT : ok I learned that python -u does the trick. But originally my python script has #!/bin/env python at the beginning of the script so I run it test.py | tee log. I searched the net and found I can do :

ckim@stph45:~/test] setenv PYTHONUNBUFFERD 1
ckim@stph45:~/test] $test.py | tee log

Solution

  • Use -u to unbuffer,

    python -u test.py | tee log