How to better control print and stdout?

Drew Smathers drew.smathers at earthlink.net
Wed Mar 12 21:20:55 EST 2003


Hello,

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
		else:
			print '\\',
			phase = 0
		time.sleep(interval)
		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