473,413 Members | 1,700 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,413 software developers and data experts.

Printing/updating output to the screen

Hi there. I hope this isn't a stupid question to ask, but does anyone
know how to print out a string without moving to a new line each time
and simply updating the first line. An example would be, if I wanted to
have a percentage progress counter that was constantly updating. I'm
unsure how to do this without printing to a brand new line. Any help
would be greatly appreciated. Thanks.

Daniel

Jul 18 '05 #1
3 4082
Daniel,

Here is an example progressbar class that
I use in my programs.

-Larry

class progressbarClass:
def __init__(self, finalcount):
import sys
self.finalcount=finalcount
self.blockcount=0
self.block="*"
self.f=sys.stdout
#
# 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):
if (count > self.finalcount): count=self.finalcount
if (self.finalcount != 0) :
percentcomplete=int(round(100*count/self.finalcount))

if (percentcomplete < 1):
percentcomplete=1
else:
percentcomplete=100

blockcount=int(percentcomplete/2)
if (blockcount > self.blockcount):
for i in range(self.blockcount,blockcount):
self.f.write(self.block)
self.f.flush()

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

self.blockcount=blockcount
return

if __name__ == "__main__":
from time import sleep
pb=progressbarClass(8)
count=0
while count<9:
count+=1
pb.progress(count)
sleep(0.2)

pb=progressbarClass(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=progressbarClass(1)
pb.progress(1)

"Daniel Pryde" <dp***********@cis.strath.ac.uk> wrote in message
news:40********@nntphost.cis.strath.ac.uk...
Hi there. I hope this isn't a stupid question to ask, but does anyone
know how to print out a string without moving to a new line each time
and simply updating the first line. An example would be, if I wanted to
have a percentage progress counter that was constantly updating. I'm
unsure how to do this without printing to a brand new line. Any help
would be greatly appreciated. Thanks.

Daniel

Jul 18 '05 #2
Daniel Pryde <dp***********@cis.strath.ac.uk> wrote in message news:<40********@nntphost.cis.strath.ac.uk>...
Hi there. I hope this isn't a stupid question to ask, but does anyone
know how to print out a string without moving to a new line each time
and simply updating the first line. An example would be, if I wanted to
have a percentage progress counter that was constantly updating. I'm
unsure how to do this without printing to a brand new line. Any help
would be greatly appreciated. Thanks.

Daniel


Put a comma after it. For example:

print "hi",

This, however, also prints a space. If that's not what you want, do the following:

from sys import stdout
stdout.write("hi")

That won't append a space or a newline.
Another Daniel
Jul 18 '05 #3
Daniel Pryde wrote:
Hi there. I hope this isn't a stupid question to ask, but does anyone
know how to print out a string without moving to a new line each time
and simply updating the first line. An example would be, if I wanted to
have a percentage progress counter that was constantly updating. I'm
unsure how to do this without printing to a brand new line. Any help
would be greatly appreciated. Thanks.

Daniel


try this...

import time

for i in xrange(101):
print i, '\r',
time.sleep(.1)

- Josiah
Jul 18 '05 #4

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

Similar topics

8
by: Tinus | last post by:
Hello all, Because you have been so helpfull the last couple of times, I thought after testing and wasting more than 20 pages (and google-ling for 3 days :-( ). I would ask you again for your...
7
by: DazedAndConfused | last post by:
I have a 8.5 x 11 landscape document with about 1/4 inch of space on the left and right where there is no print. The document displays perfect in print preview, but when I print it, about 1/2 inch...
3
by: Birky | last post by:
I have a report which I am opening from a command button within a form. I am using the “DoCmd.OpenReport stDocName” command to open the report but it is automatically printing instead of popping the...
3
by: Pappy | last post by:
SHORT VERSION: Python File B changes sys.stdout to a file so all 'prints' are written to the file. Python file A launches python file B with os.popen("./B 2>&^1 >dev/null &"). Python B's output...
1
by: mehdi | last post by:
Hi, Consider a printing scenario where I have to draw the entire page on a 827x1169 (.01 inch) size. Thereafter, the entire bitmap has to be resized to fill a given Bounds rectangle (keeping the...
4
by: sophie_newbie | last post by:
Hi, in my program i need to call a couple of functions that do some stuff but they always print their output on screen. But I don't want them to print anything on the screen. Is there any way I can...
5
by: Vani Perumal | last post by:
Hai All, I am working on Fingerprints. I reproduced fingerprints in C. I want to print it and also for matching I want to store the output as a JPEG file. For printing I tried PrintScreen, I...
10
by: sophie_newbie | last post by:
Hi, I'm trying to write a piece of code that spawns a thread and prints dots every half second until the thread spawned is finished. Code is something like this: import threading class...
7
by: Iain Wilson | last post by:
I am pulling my hair out trying to print various objects from a .net web page My apologies for cross posting but I need an answer and my previous post has attracted no interest. ASP.Net 2.0...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...
0
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,...
0
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...

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.