Dante Arcese Dante Arcese - 4 years ago 103
Python Question

Delete Previous Line And Replace It With New Line?

My current code is the following:

for a in xrange (60, 0, -1):
b = "Sleeping for " + str(a) + " seconds\r"
print b,
sleep(1)


The only issue I have with it is that when it goes from 2 digit numbers to 1 digit numbers it prints secondss instead of seconds. How can I modify my code to properly replace the line when a goes from 2 digits to 1. Also, after the script is done printing the "Sleeping for __ seconds" lines am I able to replace that with a line that says "Sleeping for 60 seconds complete"

Answer Source

How about using string formatting?

for a in xrange (60, 0, -1):  
    print "Sleeping for %2d seconds...\r" % a, 
    sleep(1)
print "Sleeping for 60 seconds complete!"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download