Using tqdm with downloading text from UniProt - response not written in target file

38 Views Asked by At

I have written a script to download fasta sequences from UniProt. Now I am trying to add a progress bar to download. So I am trying to use tqdm for adding a progress bar. To begin with, I am using one URL to download one sequence at a time. Here is the script, that I copied from tqdm examples:

import requests, os
from tqdm import tqdm

URL = 'https://rest.uniprot.org/uniprotkb/P06875.fasta'
response = requests.get(URL, stream=True)
with tqdm.wrapattr(open('output.txt', "ab"), "write",
                   miniters=1, desc=URL.split('/')[-1],
                   total=int(response.headers.get('content-length', 0))) as bar:
    for chunk in response.iter_content(chunk_size=4096):
        bar.write(chunk)

The code gives me this output:

P06875.fasta:   0%|          | 0/947 [00:00<?, ?it/s]
P06875.fasta: 100%|██████████| 947/947 [00:00<00:00, 310kB/s]

It works to show this progress bar for the downloading part. The output.txt file is created but nothing is written in it. I believe I am missing something important here. Ultimately, for my final script, which has a for loop going through each URL to download all the sequences one by one, I would like to incorporate this for the whole list of URLs.

Thanks.

0

There are 0 best solutions below