lenooh satisfied my query. I discovered this article while searching for 'python suppress newline'. I'm using IDLE 3 on Raspberry Pi to develop Python 3.2 for PuTTY.
I wanted to create a progress bar on the PuTTY command line. I didn't want the page scrolling away. I wanted a horizontal line to reassure the user from freaking out that the program hasn't cruncxed to a halt nor been sent to lunch on a merry infinite loop - as a plea to 'leave me be, I'm doing fine, but this may take some time.' interactive message - like a progress bar in text.
The print('Skimming for', search_string, '\b! .001', end='')
initializes the message by preparing for the next screen-write, which will print three backspaces as ⌫⌫⌫ rubout and then a period, wiping off '001' and extending the line of periods.
After search_string
parrots user input, the \b!
trims the exclamation point of my search_string
text to back over the space which print()
otherwise forces, properly placing the punctuation. That's followed by a space and the first 'dot' of the 'progress bar' which I'm simulating.
Unnecessarily, the message is also then primed with the page number (formatted to a length of three with leading zeros) to take notice from the user that progress is being processed and which will also reflect the count of periods we will later build out to the right.
import sys
page=1
search_string=input('Search for?',)
print('Skimming for', search_string, '\b! .001', end='')
sys.stdout.flush() # the print function with an end='' won't print unless forced
while page:
# some stuff…
# search, scrub, and build bulk output list[], count items,
# set done flag True
page=page+1 #done flag set in 'some_stuff'
sys.stdout.write('\b\b\b.'+format(page, '03')) #<-- here's the progress bar meat
sys.stdout.flush()
if done: #( flag alternative to break, exit or quit)
print('\nSorting', item_count, 'items')
page=0 # exits the 'while page' loop
list.sort()
for item_count in range(0, items)
print(list[item_count])
#print footers here
if not (len(list)==items):
print('#error_handler')
The progress bar meat is in the sys.stdout.write('\b\b\b.'+format(page, '03'))
line. First, to erase to the left, it backs up the cursor over the three numeric characters with the '\b\b\b' as ⌫⌫⌫ rubout and drops a new period to add to the progress bar length. Then it writes three digits of the page it has progressed to so far. Because sys.stdout.write()
waits for a full buffer or the output channel to close, the sys.stdout.flush()
forces the immediate write. sys.stdout.flush()
is built into the end of print()
which is bypassed with print(txt, end='' )
. Then the code loops through its mundane time intensive operations while it prints nothing more until it returns here to wipe three digits back, add a period and write three digits again, incremented.
The three digits wiped and rewritten is by no means necessary - it's just a flourish which exemplifies sys.stdout.write()
versus print()
. You could just as easily prime with a period and forget the three fancy backslash-b ⌫ backspaces (of course not writing formatted page counts as well) by just printing the period bar longer by one each time through - without spaces or newlines using just the sys.stdout.write('.'); sys.stdout.flush()
pair.
Please note that the Raspberry Pi IDLE 3 Python shell does not honor the backspace as ⌫ rubout, but instead prints a space, creating an apparent list of fractions instead.
print("." * 10)
? – Durstsep
andend
in python print statement – Marlonmarlow