Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I show never ending dots ("...") in a while loop [duplicate]

Tags:

python

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

like image 671
Brandon Usher Avatar asked May 26 '15 19:05

Brandon Usher


People also ask

Do while loop never ends?

Infinite while loop refers to a while loop where the while condition never becomes false. When a condition never becomes false, the program enters the loop and keeps repeating that same block of code over and over again, and the loop never ends.

How do you stop a while loop from repeating?

To break out of a while loop, you can use the endloop, continue, resume, or return statement. endwhile; If the name is empty, the other statements are not executed in that pass through the loop, and the entire loop is closed.


1 Answers

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)
like image 156
Ignacio Vazquez-Abrams Avatar answered Oct 19 '22 23:10

Ignacio Vazquez-Abrams