使用 tqdm 从 UniProt 下载文本 - 响应未写入目标文件

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

提问人:Irfan 提问时间:8/30/2023 最后编辑:Irfan 更新时间:8/30/2023 访问量:26

问:

我写了一个脚本来从 UniProt 下载 fasta 序列。现在我正在尝试添加一个进度条进行下载。所以我正在尝试使用 tqdm 添加进度条。首先,我使用一个 URL 一次下载一个序列。这是我从 tqdm 示例中复制的脚本:

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)

代码给了我这个输出:

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

它可以显示下载部分的此进度条。 已创建输出 .txt 文件,但其中未写入任何内容。我相信我在这里错过了一些重要的东西。 最终,对于我的最终脚本,它有一个 for 循环遍历每个 URL 以逐个下载所有序列,我想将其合并到整个 URL 列表中。

谢谢。

python 下载 进度 fasta tqdm

评论


答: 暂无答案