[Tutor] Displaying Status on the Command Line
Avi Gross
avigross at verizon.net
Wed Nov 7 22:50:09 EST 2018
What Alan wrote makes sense if you just want to put out one mark per second
till you stop.
But if you want a percentage of progress, you need some way to estimate what
percent of the way you are to being done. You need to determine how many
marks represent 100% such as 50 periods. You need to have your code pause
periodically and assess if the current percentage requires more characters
than have been put out so far. If you were at 10% last time and had put out
5 dots and are now at 16% and need 3 more dots, you might say something like
this with n=3, and ch="."
print(ch*n, end="", flush=True), sep="")
Note you want to make sure the buffers are flushed to make it real time and
when done may want to print a newline so any further output is not left
dangling.
The above print statement is for Python 3.x, and as Alan explained, in
version 2.X you may need to use alternate syntax. Not sure how you flush
buffers but you can skip the print statement and write in other ways to
sys.stdout.
-----Original Message-----
From: Tutor <tutor-bounces+avigross=verizon.net at python.org> On Behalf Of
Alan Gauld via Tutor
Sent: Wednesday, November 7, 2018 2:15 PM
To: tutor at python.org
Subject: Re: [Tutor] Displaying Status on the Command Line
On 07/11/2018 16:22, Chip Wachob wrote:
> What I would like to do is display, on a single line, in the terminal
> / command line a progress percentage, or, simply a sequence of - / -
> \, etc.. or even, accumulating period characters.
>
> What would the escape codes be, or is there a better way to handle this?
It depends on your Python version.
In Python v2 you simply put a comma after your output character to turn off
the auto newline
while someProcess(): # should probably be in a separate thread...
time.sleep(1) # 1 second pause
print '.', # comma suppresses newline
If you want to suppress the spaces tyoo things get a tad more complex and
you are probably best writing direct to sys.stdout
In Python 3 there are parameters to print()
while someProcess():
time.sleep(1)
print('.', end='', sep='') # no newline and no spaces
You shouldn't need any special escape codes.
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos
More information about the Tutor
mailing list