Rewriting output on the same line?

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Feb 2009
Posts: 162
Reputation: mahela007 is an unknown quantity at this point 
Solved Threads: 1
mahela007 mahela007 is offline Offline
Junior Poster

Rewriting output on the same line?

 
0
  #1
Nov 9th, 2009
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..
"The man who smiles when something goes wrong has thought of someone else to blame"; One of my friends said this on facebook.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,072
Reputation: jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough 
Solved Threads: 269
Sponsor
jlm699's Avatar
jlm699 jlm699 is offline Offline
Knows where his Towel is
 
0
  #2
Nov 9th, 2009
Originally Posted by mahela007 View Post
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
1. Use Code Tags.
2. Homework? Show Effort.
3. Keep discussions on the forum: no PMs
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 92
Reputation: thines01 is an unknown quantity at this point 
Solved Threads: 8
thines01 thines01 is offline Offline
Junior Poster in Training
 
0
  #3
Nov 9th, 2009
You might be looking for something like this:
  1. import sys
  2.  
  3. intLoop=0
  4. while intLoop < 100000:
  5. sys.stdout.write("%07d\b\b\b\b\b\b\b" % intLoop)
  6. intLoop += 1
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 162
Reputation: mahela007 is an unknown quantity at this point 
Solved Threads: 1
mahela007 mahela007 is offline Offline
Junior Poster
 
0
  #4
Nov 9th, 2009
No.. that's not it.. The new numbers keep printing on new lines.
"The man who smiles when something goes wrong has thought of someone else to blame"; One of my friends said this on facebook.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,151
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 952
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite
 
0
  #5
Nov 9th, 2009
Originally Posted by mahela007 View Post
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.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 92
Reputation: thines01 is an unknown quantity at this point 
Solved Threads: 8
thines01 thines01 is offline Offline
Junior Poster in Training
 
0
  #6
Nov 9th, 2009
I've tried it both on Windows XP and on Solaris and both work.
...although it looks better on Windows.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 148
Reputation: mn_kthompson is an unknown quantity at this point 
Solved Threads: 32
mn_kthompson mn_kthompson is offline Offline
Junior Poster
 
0
  #7
Nov 10th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 232
Reputation: sravan953 has a little shameless behaviour in the past 
Solved Threads: 28
sravan953's Avatar
sravan953 sravan953 is offline Offline
Posting Whiz in Training
 
0
  #8
Nov 10th, 2009
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...
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 162
Reputation: mahela007 is an unknown quantity at this point 
Solved Threads: 1
mahela007 mahela007 is offline Offline
Junior Poster
 
0
  #9
Nov 10th, 2009
oh.. Yeah.. it works in the command prompt. Could someone explain how this works?
"The man who smiles when something goes wrong has thought of someone else to blame"; One of my friends said this on facebook.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,072
Reputation: jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough 
Solved Threads: 269
Sponsor
jlm699's Avatar
jlm699 jlm699 is offline Offline
Knows where his Towel is
 
0
  #10
Nov 10th, 2009
Originally Posted by mahela007 View Post
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.
1. Use Code Tags.
2. Homework? Show Effort.
3. Keep discussions on the forum: no PMs
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 578 | Replies: 12
Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC