Alex Rivera | Logout

How to create a spinning command line cursor?

Asked 2011-02-14T18:17:09.913
102

Is there a way to print a spinning cursor in a terminal using Python?

Edit
Report

1 Answer

1
#!/usr/bin/env python

import sys

chars = '|/-\\'

for i in xrange(1,1000):
    for c in chars:
        sys.stdout.write(c)
        sys.stdout.write('\b')
        sys.stdout.flush()

CAVEATS: In my experience this doesn't work in all terminals. A more robust way to do this under Unix/Linux, be it more complicated is to use the curses module, which doesn't work under Windows. You probably want to slow it down some how with actual processing that is going on in the background.

answered 2011-02-14T18:28:23.683

Your Answer