473,748 Members | 6,161 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to clear previous console output?

Hi, group,
May I ask a newbie question?

Given the below example:

for i in range(100):
print "%s%%" % i

I want to show the progress by print 1% to 100%. But I hope each
output will clear the previous output before print, so that I can see a
dynamic updated display of percentage, instead of print all percentages
one by one. How can I do that(what about on both windows and unix
platform)?

Thank you in advance for your expertise.

Best,

Cl

Jul 18 '05 #1
3 13941
(For console output) Use \r.

Example:

for i in range(100):
print '%s\r' % ' '*20, # clean up row
print '%d%%' % i, # note ending with comma
print

Note that it works only in real console window, not in simulated one (like
in WinIDE).
That is cross-platform.

HTH,
Mike

Newgene wrote:
I want to show the progress by print 1% to 100%. But I hope each
output will clear the previous output before print, so that I can see
a dynamic updated display of percentage, instead of print all


Jul 18 '05 #2
Try this class I use on both Linux and Windows
(console apps). See main program for example
usage.

HTH,
Larry Bates
class progressbarClas s:
def __init__(self, finalcount, progresschar=No ne):
import sys
self.finalcount =finalcount
self.blockcount =0
#
# See if caller passed me a character to use on the
# progress bar (like "*"). If not use the block
# character that makes it look like a real progress
# bar.
#
if not progresschar: self.block=chr( 178)
else: self.block=prog resschar
#
# Get pointer to sys.stdout so I can use the write/flush
# methods to display the progress bar.
#
self.f=sys.stdo ut
#
# If the final count is zero, don't start the progress gauge
#
if not self.finalcount : return
self.f.write('\ n------------------ %
Progress -------------------1\n')
self.f.write(' 1 2 3 4 5 6 7 8 9 0\n')
self.f.write('----0----0----0----0----0----0----0----0----0----0\n')
return

def progress(self, count):
#
# Make sure I don't try to go off the end (e.g. >100%)
#
count=min(count , self.finalcount )
#
# If finalcount is zero, I'm done
#
if self.finalcount :
percentcomplete =int(round(100* count/self.finalcount ))
if percentcomplete < 1: percentcomplete =1
else:
percentcomplete =100

#print "percentcomplet e=",percentcomp lete
blockcount=int( percentcomplete/2)
#print "blockcount=",b lockcount
if blockcount > self.blockcount :
for i in range(self.bloc kcount,blockcou nt):
self.f.write(se lf.block)
self.f.flush()

if percentcomplete == 100: self.f.write("\ n")
self.blockcount =blockcount
return

if __name__ == "__main__":
from time import sleep
pb=progressbarC lass(8,"*")
count=0
while count<9:
count+=1
pb.progress(cou nt)
sleep(0.2)

pb=progressbarC lass(100)
pb.progress(20)
sleep(0.2)
pb.progress(47)
sleep(0.2)
pb.progress(90)
sleep(0.2)
pb.progress(100 )
print "testing 1:"
pb=progressbarC lass(1)
pb.progress(1)

"Newgene" <ne*****@bigfoo t.com> wrote in message
news:ce******** @odbk17.prod.go ogle.com...
Hi, group,
May I ask a newbie question?

Given the below example:

for i in range(100):
print "%s%%" % i

I want to show the progress by print 1% to 100%. But I hope each
output will clear the previous output before print, so that I can see a
dynamic updated display of percentage, instead of print all percentages
one by one. How can I do that(what about on both windows and unix
platform)?

Thank you in advance for your expertise.

Best,

Cl

Jul 18 '05 #3

"Mike Rovner" <mi**@nospam.co m> wrote in message
news:ce******** **@sea.gmane.or g...
(For console output) Use \r.

Example:

for i in range(100):
print '%s\r' % ' '*20, # clean up row
print '%d%%' % i, # note ending with comma
print

Note that it works only in real console window, not in simulated one (like in WinIDE).
That is cross-platform.


If you do not want to erase everything, \10 in string inserts backspace
character, but blanking and rewriting everything may be easier.

tjr

Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
2336
by: AMeador | last post by:
I can see console output in the output window on VS, but how can you see this text when running the app on a different machine or outside of VS? Thanks! --- Andrew
1
2131
by: cplusplusstudent | last post by:
Hello I am a new C++ programmer and am working with the Visual C++ studio. I am attempting to create a simple console application for myself but I fin that neither of the introductory C++ books I own tell me how to clear the console screen or change the font color. Is there some way to do this that could b understood by a novice programmer? Here are the three things I would like to do: Control the size of the console screen when the...
3
4960
by: Jason | last post by:
I've got a simple program in which I just write something to the console output window, but when I compile and run the program, the output windows displays the output and closes right away. How do I configure it to have the console window stay open so I can view the output without it closing. Thanks Jason
4
1544
by: djc | last post by:
is this possible: lets say my output has 2 columns with headings like 'host' and 'status'. host status ------------ host1 down host2 up host3 up host4 down
0
1389
by: Helge Lenuweit | last post by:
Hello, I am trying to put together an application that (1) shows Console.WriteLine() output when launched from within a console window (that is, from the command prompt) but that (2) **does not open** a console window when launched from Explorer or another process. Can this be done? I haven't had any luck so far - either all Console output is redirected to null (target:winexe) or a console window is
3
3470
by: Gureet Singh | last post by:
Hi This is my first day with C++. Iwas running the following program #include<iostream.h> #include<conio.h> main() { int x,y,z; cout<< " input x,y \n"; cin>> x >> y;
4
2261
by: =?Utf-8?B?RGF2aWQgVGhpZWxlbg==?= | last post by:
Hi; When my ASP.NET app is running on IIS, where does the Console.Out.WriteLine("hi there"); output go? -- thanks - dave david_at_windward_dot_net http://www.windwardreports.com
3
13859
by: TC | last post by:
I'm trying to debug a console application, but I can't see the console output. I've seen many references which say that console output is supposed to appear on the Output window when the application is run in Debug mode. However, I just can't get that to work. I'm using Visual Studio 2005. I've confirmed that my application is compiled as a console application, and that I'm running in Debug mode. To investigate this issue, I've reduced...
27
2964
by: CarlosMB | last post by:
Hello, I am writing code that uses a DLL which is supposed to print to console some useful information but for some reason it is not doing so. The environment is a bit complex to explain but here it goes: - I am using a C library called SYMPHONY, which I compiled myself. When using that
0
9537
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9367
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9319
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9243
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8241
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6795
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6073
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
2780
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2213
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.