osx - Python sys.stdout.write() strange behavior in Mac terminal -


i trying make progress bar process, keep things short consider following snippet:

import sys import time  in range(10):   time.sleep(0.5)   sys.stdout.write('*') sys.stdout.write('\n') 

what expecting print star every half second on same line (which when run code ide sublime text example). however, when run in mac os terminal waits 5 seconds prints whole string ********** @ once.

any ideas why might happening , how desired behavior?

it because output buffered. try instead:

import sys import time  in range(10):     time.sleep(0.5)     sys.stdout.write('*')     sys.stdout.flush()    # <-- key sys.stdout.write('\n') 

by flushing output each time write, sys.stdout won't wait next newline; instead write whatever characters in buffers immediately.


Comments

Popular posts from this blog

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

java - Could not locate OpenAL library -

sorting - opencl Bitonic sort with 64 bits keys -