Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python sys.stdout.flush() doesn't work

The following code is supposed to print from 1 to 10 with 1 second intervals in between, however it is waiting for 10 seconds before actually printing anything and then it prints it all at once. How can I unbuffer the output?

import sys
import time
for count in range(10) :
   sys.stdout.write(str(count))
   sys.stdout.flush()
   time.sleep(1)
like image 312
user1724351 Avatar asked Oct 06 '12 00:10

user1724351


1 Answers

Found the problem

import sys
import time
for count in range(10) :
   sys.stdout.write("\b%s" % count)
   sys.stdout.flush()
   time.sleep(.1)

Don't know why python is weird like this but apparently it accepted this. Your code should've worked fine but I guess python just didn't like you.

like image 106
Alexander Avatar answered Nov 10 '22 06:11

Alexander