Reputation: 91
I have seen many questions on stackoverflow but none of the answers give a simple elegant method.
link = "http://download.thinkbroadband.com/10MB.zip"
file_name = "test"
with open(file_name, "wb") as f:
print('Downloading: {}'.format(file_name))
response = requests.get(link, stream=True)
total_length = response.headers.get('content-length')
if total_length is None:
f.write(response.content)
else:
dl = 0
total_length = int(total_length)
for data in response.iter_content(chunk_size=4096):
dl += len(data)
f.write(data)
done = int(50 * dl / total_length)
sys.stdout.write("\r[%s%s]" % ('=' * done, ' ' * (50-done)))
sys.stdout.flush()
Can I get something with more details when a file is downloaded? All the previous questions do not have a good simple answer.
Upvotes: 0
Views: 1858
Reputation: 326
Why reinvent the wheel? Use tqdm. Follow the link and follow the instructions to import tqdm and add a progress bar for any iteration. For example:
from tqdm import tqdm
...
for data in tqdm(response.iter_content(chunk_size=4096)):
# additional logic here
...
Read the examples in the provided pypi link to add additional information to your progress bar.
Upvotes: 2