What's Wrong with Tee Command?
I Am Running the Following Python Script Piped to Tee Command #! /Usr/Bin/Python from Sys Import Exit, Exc_Info from Time Import Sleep Try: Print "Hello"...
I am running the following python script piped to tee command
#!/usr/bin/python
from sys import exit,exc_info
from time import sleep
try:
print "hello"
#raise KeyboardInterrupt
while True:
print "hello"
sleep(1)
except KeyboardInterrupt:
print "Key board Interrupt"
exit(0)
Let's say I stored this in file.py
Now if I execute:
./file.py | tee somefile
Now press Ctrl+C, observe nothing is printed into the somefile and stdout
Under normal execution:
./file.py
Upon Ctrl+C:
hello
hello
^CKey board Interrupt
Also file redirection is working fine. What's wrong with tee
2 Answers
Nothing's wrong with tee. Python buffers output if it detects it's not writing to a TTY. See this Unix & Linux post. Use sys.stdout.flush() to force flushing the buffer.
You can use this way as well:
python -u file.py | tee somefile
This problem mentioned on stackoverflow