Skip to content

Instantly share code, notes, and snippets.

@alx-xlx
Last active February 18, 2020 22:21
Show Gist options
  • Save alx-xlx/4d9692335839b2bf53e648d0655210fb to your computer and use it in GitHub Desktop.
Save alx-xlx/4d9692335839b2bf53e648d0655210fb to your computer and use it in GitHub Desktop.
Progress bar for Python Downloads
import os, sys, tqdm, requests
URL = "https://file-examples.com/wp-content/uploads/2017/11/file_example_MP3_700KB.mp3"
r = requests.get(URL, stream=True)
# tqdm Progress Bar Magic
total_size = int(r.headers.get('content-length',0))
block_size = 1024
t=tqdm(total=total_size, unit='iB', unit_scale=True)
with open('file_example.mp3', 'wb') as f:
for data in r.iter_content(block_size):
t.update(len(data))
f.write(data)
t.close()
#move cursor up one line
sys.stdout.write('\x1b[1A')
#now delete the output i.e in our case the 100% Progress bar
sys.stdout.write('\x1b[2K')
# Just in case the file is not online
if total_size != 0 and t.n != total_size:
print("ERROR, something went wrong")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment