Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

print progress counter of python in DOS

enter image description here

test_count = 0

while test_count <= 100:
    print test_count
    test_count +=1

Currently this counter is printing on next line but I am looking for way to overwrite it on "0".

like image 910
Deepak Dubey Avatar asked May 14 '26 19:05

Deepak Dubey


2 Answers

Use the \r carriage-return character with sys.stdout.flush.

import sys
import time  # for invoking time.sleep(n_seconds) inside loop

counter = 0
while counter <= 100:
    time.sleep(1)
    counter += 1
    sys.stdout.write("\rTesting (%ss elapsed)" % counter)
    sys.stdout.flush()
like image 177
pztrick Avatar answered May 16 '26 08:05

pztrick


Use \r and add a , at the end of your print statement to not automatically write a newline as in the code below.

Also, see the python-progressbar library for some nice text implementations of progress bars.

import time # Added to demonstrate effect

test_count = 0

while test_count <= 100:
    print "\r%3d" % test_count,
    time.sleep(0.1)
    test_count +=1
like image 36
Dhara Avatar answered May 16 '26 07:05

Dhara



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!