How to better control print and stdout?

Drew Smathers drew.smathers at
Thu Mar 13 03:20:55 CET 2003


I'm new to python and have been having a hell of a time using stdout.  Obviously Python has a unique way of printing strings.

Basically, all I want to do is be able to make nice countdowns, or the classic spinner (|,/,-,\), using the escape code '\b'.

This following function is an example:

def spinner(rotations, interval):
	phase = 0
	for i in range(rotations):
		if phase == 0:
			print '|',
                        phase = phase + 1
		elif phase == 1:
			print '/',
                        phase = phase + 1
		elif phase == 2:
			print '-',
                        phase = phase + 1
			print '\\',
			phase = 0
		print '\b\b',
	print ''

The program waits for the loop to end before printing anything.
(If I omit the comma, this results in the program printing in sync with the loop but throws in the unwanted newline of course.) 

Is there anyway to get around this without using any fancy libs like ncurses?

I would appreciate any help...

More information about the Python-list mailing list