为多个文件下载添加进度条

问题描述 投票:0回答:0

我正在尝试制作一个简单的脚本来从 UniProt 数据库下载多个蛋白质序列。到目前为止,我已经做得足够好了。该脚本将读取每行包含登录号的文本文件,读取该文件,然后创建相应的 URL,下载序列,写入同一文件中。所以,最后我得到一个文件,其中所有序列依次写入。

现在,我想添加一个下载进度条。我搜索了论坛,发现了一些主要用于下载一个文件的选项。我的序列有时超过 500 个,我想添加一个下载进度条,以便我可以查看下载了多少。我插入了我找到的“clint”代码,但每次它通过 for 循环时都会写入 for 。

这是代码:

import time
begin = round (time.time(),1)
import requests
from clint.textui import progress

#Change the name of file containing Uniprot accession IDs
with open ('sampleids.txt', 'r') as infile:
    lines = infile.readlines()
listfile_name = infile.name
file_name = listfile_name.split('.', 1)[0]
#print (file_name)
count = 0

with open ((file_name)+'_sequences.fa', 'wb') as txtfile:
    for line in lines:
        count+=1
        line = line.strip()
        access_id = line
        url_part1 = 'https://rest.uniprot.org/uniprotkb/'
        url_part2 = '.fasta'
        #get the sequences from the url
        URL = url_part1+access_id+url_part2      
        response = requests.get (URL,stream=True)                  
        txtfile.write(response.content)
        total_length = int(response.headers.get('content-length'))
        for chunk in progress.bar(response.iter_content(chunk_size=1024), expected_size=(total_length/1024)+1):
            if chunk:
                txtfile.flush()
print ("Total sequences downloaded = ", count)
time.sleep(1)
end = round (time.time(),1)
print(f"Time taken = {end - begin} seconds")

是否可以正确修改它,这样它就可以给我完整下载的进度,而不是像这样:

输出截图: screenshot of output

搜索论坛并找到多种解决方案,但只能下载单个文件。

python download progress-bar clint
© www.soinside.com 2019 - 2024. All rights reserved.