5

Is it possible to show something like this in Python?

Checking.

Checking..

Checking...

Checking.

I want it to just show one line of this while the script is running, but stop when it's done. The script I want to add it to is here: https://github.com/brandonusher/Python-Scripts/blob/master/check_port.py

Community
  • 1
  • 1
  • 2
    [This](http://stackoverflow.com/questions/5290994/python-remove-and-replace-printed-items) might help. – Kamehameha May 26 '15 at 19:07
  • 1
    in console, you can delete a character by outputing `\b`. – Jason Hu May 26 '15 at 19:07
  • 1
    Kevin, I think the duplicate linked by @Kamehameha is better -- there are several answers there and the duplicate linked there has even more answers, including one identical to the one you linked to. – wflynny May 26 '15 at 19:28

1 Answers1

12

Output the text with no newline character, then flush stdout. Output a \r to make the cursor go back to the beginning of the line. Repeat until done. Don't forget to overwrite existing text.

while True:
  sys.stdout.write('\rfoo.  ')
  sys.stdout.flush()
  delay(100)
  sys.stdout.write('\rfoo.. ')
  sys.stdout.flush()
  delay(100)
  sys.stdout.write('\rfoo...')
  sys.stdout.flush()
  delay(100)
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358