Problems with sys.stdout.write() with time.sleep() in a function
Posted
by philipjkim
on Stack Overflow
See other posts from Stack Overflow
or by philipjkim
Published on 2010-05-11T07:38:51Z
Indexed on
2010/05/11
7:44 UTC
Read the original article
Hit count: 138
python
What I wanted is printing out 5 dots that a dot printed per a second using time.sleep(), but the result was 5 dots were printed at once after 5 seconds delay.
Tried both print and sys.stdout.write, same result.
Thanks for any advices.
import time
import sys
def wait_for(n):
"""Wait for {n} seconds. {n} should be an integer greater than 0."""
if not isinstance(n, int):
print 'n in wait_for(n) should be an integer.'
return
elif n < 1:
print 'n in wait_for(n) should be greater than 0.'
return
for i in range(0, n):
sys.stdout.write('.')
time.sleep(1)
sys.stdout.write('\n')
def main():
wait_for(5) # FIXME: doesn't work as expected
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
print '\nAborted.'
© Stack Overflow or respective owner