defex5():
withopen('example.txt', 'r') as f:
pb5 = ProgressBar(len(f.readlines()), 'lines', pre="Reading lines from file {}".format(f.name), post='Finished reading file!')
f.seek(0) # Return to start of line after obtaining line count
pb5.print_progress() # Prints the initial empty progress barfor lineno, line inenumerate(f, start=1):
pb5.print_progress(lineno, pre=line.replace('\n', ''))
sleep(1)
Reading lines from file example.txt
Line 1
Line 2
Line 3
Line 4
...
[=================================================>] - Finished 5 lines of 5 lines
Finished reading file!
ByteProgressBar Example
import requests
from ProgressPrinter import ByteProgressBar
defex1():
# The ByteProgressBar is a extension of the normal progress bar that supports automatic unit conversion# from bytes, the base unit you will work with most of the time, to KB, MB, etc.# This for example is useful when downloading or working with a file (see example below)## UNCOMMENT TO LINK YOU WANT TO USE FOR THE TEST DOWNLOAD# -------------------------------------------------------# 100MB.bin [100 MB] - Auto conversion to MB# link = 'https://speed.hetzner.de/100MB.bin'# -------------------------------------------------------------------------------------------# Linux Mint Cinnamon x64.iso [ca. 1.87 GB] - Auto conversion to GB
link ='http://mirrors.evowise.com/linuxmint/stable/19.2/linuxmint-19.2-cinnamon-64bit.iso'# -------------------------------------------------------------------------------------------
file_name = link.split('/')[-1]
withopen(file_name, 'wb') as f:
response = requests.get(link, stream=True)
byte_size =int(response.headers.get('content-length'))
if byte_size isNone:
f.write(response.content)
else:
# Set to ByteProgressBar.UNITS.AUTO to automatically interfere a fitting unit from the byte size# -----------------------------------------------------------------------------------------------# Supported values: ByteProgressBar.UNITS.{BYTES, KILOBYTES, MEGABYTES, GIGABYTES, TERABYTES, AUTO}
bpb1 = ByteProgressBar(byte_size, ByteProgressBar.UNITS.AUTO, pre='Downloading {} from {}'
.format(file_name, link), post='Finished download!')
bpb1.print_progress()
loaded_bytes =0for chunk in response.iter_content(chunk_size=1048576): # 1 MB
loaded_bytes +=len(chunk)
f.write(chunk)
bpb1.print_progress(loaded_bytes)
Downloading linuxmint-19.2-cinnamon-64bit.iso from http://mirrors.evowise.com/linuxmint/stable/19.2/linuxmint-19.2-cinnamon-64bit.iso
[======> ] - Finished 0.27 GB of 1.87 GB
The Tidelift Subscription provides access to a continuously curated stream of human-researched and maintainer-verified data on open source packages and their licenses, releases, vulnerabilities, and development practices.