Is it possible to keep writing the output of a program on the same line instead of moving to a new line every time? The output should be written over the preceding output. An example would be a kind of counter...
say a number that counts from 1 to 10 ...
The normal output of a while loop would be
1
2
3
4
5
etc..

What I want is something that will display 1.. erase it and then display 2.. erase that and display 3 and so on..

Is it possible to keep writing the output of a program on the same line instead of moving to a new line every time? The output should be written over the preceding output. An example would be a kind of counter...
say a number that counts from 1 to 10 ...
The normal output of a while loop would be
1
2
3
4
5
etc..

What I want is something that will display 1.. erase it and then display 2.. erase that and display 3 and so on..

To overwrite the previous output you could use the system command os.system('cls') on Windows or os.system('clear') on Linux.

But if you're looking for single character control you'll need to look into curses, which isn't readily available for Windows.

To print on the same line you can end your print statement with a ',' so that instead of printing a new line it prints a space... But that won't help you to clear the previous value

You might be looking for something like this:

import sys

intLoop=0
while intLoop < 100000:
   sys.stdout.write("%07d\b\b\b\b\b\b\b" % intLoop)
   intLoop += 1

No.. that's not it.. The new numbers keep printing on new lines.

No.. that's not it.. The new numbers keep printing on new lines.

If your output window is a true console window, it will work! Many IDEs have their own GUI based output windows. It wont work there.

I've tried it both on Windows XP and on Solaris and both work.
...although it looks better on Windows.

I've only done this one time, and I found that I had to use sys.stdout.flush() before I wrote the \b characters. Just in case you run into any problems.

Member Avatar for sravan953

You'll either have to follow jlm699's advice or there's no other go, UNLESS.... unless you make a GUI for your program where this is possible...

oh.. Yeah.. it works in the command prompt. Could someone explain how this works?

Could someone explain how this works?

If I'm not mistaken \b means "backspace". So in this example you're writing a seven digit numeral to the screen with %07d and then seven "backspaces" to clear those numerals before writing the next number.

ah.. thanks for the help.

Just to add on, the carriage return character '\r' actually moves the cursor to the start of the current line. So in this case you could just print the number (without a newline), wait a couple seconds, print a carriage return then print the next number.

Yes, that's how it works.
You can get fancier with it to support different size numbers, but I've shown a fairly simple method.

How would you use this for printing '.' ?

Because

import sys
import time

intLoop=0
dot = '.'
dottimes = 1

while intLoop < 5:
sys.stdout.write("%05d\b\b\b\b\b" % dot*dottimes)
intLoop += 1
dottimes += 1
time.sleep(1)

does not work...
It needs and integer
How can I get it to use strings?

wikkido5000 Satrt your own thread in which you exaplin your situation. Don't hijack 2 years old threads.
You can't mulriply a string '.' with a an integer dottimes, and why would you need that.
If you really want that overloading of the console:

import time
import sys
for i in range (0, 100):
    sys.stdout.write('%d\r' % i)
    sys.stdout.flush()
    time.sleep(0.1)
    sys.stdout.write('\b')
raw_input("Press RETURN to finish.")
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.