Chan Kim Chan Kim - 1 year ago 64
Python Question

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 :

#!/bin/env python

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

if __name__ == '__main__':
w1('press a key')

How it runs (ok) :

press a key


But when I run | 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
is not displayed on the screen when run through tee command and it was waiting for my key. How can I make this
shown even when I run it with

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 | tee log
. I searched the net and found I can do :

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

Answer Source

Use -u to unbuffer,

python -u | tee log
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download