Operation check
Raspberry Pi2 (raspbian)
MacOS X 10.8.5
Prepare the following code
hello.py
#!/usr/bin/env python
import time
print "hello"
for x in range(0,60):
print x
time.sleep(1)
Execute as follows
$ ./hello.py > log.tmp
Do the following in another terminal
$ tail -f log.tmp
When doing the above, it seems that the contents of log.tmp are updated after 60 prints </ font>.
When viewed with stdout, it is displayed every second, but when it is output to a file, it is not output every second.
Prepare a bash script instead
test-exec
#!/usr/bin/env bash
for x in $(seq 1 60)
do
echo $x
sleep 1
done
If you execute this, the file will be output every second.
How do you get a python print statement to print a file every second?
It may be possible to use a file write command other than print, but I have to find out how to combine it with file redirection.
It is regrettable that even if the temperature and humidity can be measured with DHT11, it cannot be seen with tail -f when outputting the file.
http://stackoverflow.com/questions/107705/disable-output-buffering
It seems that you can specify the -u option when running python.
hello.py
#!/usr/bin/env python -u
import time
print "hello"
for x in range(0,60):
print x
time.sleep(1)
The above specification #! / Usr / bin / env python -u
worked on MacOSX.
On the other hand, on raspberry Pi 2, I got an error like -u option is missing
.
I made #! / usr / bin / python -u
and it worked.
Recommended Posts