user1551817
user1551817

Reputation: 7451

Overwrite previous output on same line

I want the output of my code to overwrite the previous output on the same line.

I have read the previous answers to a similar question and have read that I can do this using a ',' and a '\r', but this doesn't seem to work for me. I tried:

for i in range(length):
    print 'Minutes:',minute,'of {0}'.format(length),'\r',
    minute+=1
    time.sleep(1)

But it doesn't print anything other than the last line of the loop. I've tried other arrangements,but nothing yet has worked. Could someone let me know what I'm doing wrong?

Thanks.

Upvotes: 0

Views: 3008

Answers (5)

Waxrat
Waxrat

Reputation: 2185

Try flushing the output before each sleep.

minute+=1
sys.stdout.flush()
time.sleep(1)

Upvotes: 0

Chiheb Nexus
Chiheb Nexus

Reputation: 9257

If you are using Python3, you can use a code like this:

import time

minute, length = 1, 100
for i in range(length):
    print ('Minutes: {0} of {1}\r'.format(minute, length), end = "") 
    minute+=1
    time.sleep(1)

However if you are using Python2, you can import print_function from __future__ module like this example:

from __future__ import print_function
import time

minute, length = 1, 100
for i in range(length):
    print("Minutes: {0} of {1}\r".format(minute, length), end = "")
    minute+=1
    time.sleep(1)

PS: I have a strange issue when running the last code from my terminal using Python2.7.10. The script work but there is not any output.

However within Python 2.7.10 interpreter the code works fine.

Test both solutions and leave your feedbacks if you encounter any problems within Python2.

EDIT:

I think the better solution to avoid the strange issue that i encounter, and i don't know the cause, is using the ASCII escape as @Fejs said in his answer.

Your code will be something like this:

import time

minute, length = 1, 100
for i in range(length):
    print "Minutes: {0} of {1} {2}".format(minute, length, '\033[1A\r')
    minute+=1
    time.sleep(1)

Upvotes: 0

Jason
Jason

Reputation: 2304

The easiest way I can think of doing this is, if you know how many lines your shell is, then you can just

print "\n" * (number_of_lines - 1)

then

print 'Minutes:',minute,'of {0}'.format(length)

So together,

for i in range(length):

    print "\n" * (number_of_lines - 1)
    print 'Minutes:',minute,'of {0}'.format(length)
    minute += 1
    time.sleep(1)

General Tips

  1. You use commas and str.format() in the same print statement, instead just use str.format() for all of it. e.g print 'Minutes: {0}, of {1}'.format(minute, length).

  2. You used minute as your counter even though it appears you are counting by seconds. For clarity you may want to rename that variable second.


Note

sys.stderr is the better way to do this. Please look at rth's answer

Upvotes: 0

rth
rth

Reputation: 3106

You need sys.stderr for fast output on a screen:

import sys,time
length,minute = 10,0
for i in range(length):
    sys.stderr.write('Minutes:{} of {}\r'.format(minute,length))
    minute+=1
    time.sleep(1)

Don't forget to add sys.stderr.write('\n') at the end of your loop to avoid printing into the same line.

Upvotes: 1

Fejs
Fejs

Reputation: 2888

If You are doing this in Linux, You can simply use ASCII escape sequence to move cursor up one line (\033[1A). Of course, You will still use \r to move to the beginning of the line. You could use something like this:

for i in range(length):
    print('Minutes: ' + minutes + '\033[1A\r')
    minutes += 1
    sleep(1)

Upvotes: 1

Related Questions