本文介绍了Python 3:如何创建用于下载文件的文本进度栏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有这个:

def download_dropbox(url, pre_file_name):
    file = url[42:]
    file = file[:-5]
    file_name = pre_file_name + file
    print('Downloading from ' + url + ' to ' + file_name)
    print(file)
    u = urllib.request.urlopen(url)
    data = u.read()
    u.close()

    with open(file_name, "wb") as f:
        f.write(data)
    print('Download Completed from ' + url + ' and saved to ' + file_name)

这基本上是从保管箱下载文件并将其保存到目录中.但是我希望能够拥有某种文本进度条,例如:

This basically downloads files from dropbox and saves it to a directory. However I want to be able to have some sort of text progress bar like:

[====] 50%

[==== ]50%

OR

50%

我认为最困难的部分是使用任何外部模块(例如,加载栏模块等)来完成它.此外,如标题所述,我需要在python 3中使用它.谢谢.

The hard part i would think is doing it with any external modules like the loading bar module, etc. Also, as the title states, I need it in python 3. Thank-you.

感谢Martin Evans在循环和进度栏读取数据,这是代码的最终结果:

Thanks to Martin Evans for the data read while loop and progress bar here is the end result of the code:

#Get the total number of bytes of the file to download before downloading
print ("opening url:", url)
u = urllib.request.urlopen(url)
meta = u.info()
print(str(meta).split())
metaInfo = str(meta).split()
print(len(metaInfo))
print ("Content-Length:" + metaInfo[46] + " bytes")
fileTotalbytes=int(metaInfo[46])

data_blocks = []
# total = int(metaInfo[46])
total=0

while True:
    block = u.read(1024)
    data_blocks.append(block)
    total += len(block)
    hash = ((60*total)//fileTotalbytes)
    print("[{}{}] {}%".format('#' * hash, ' ' * (60-hash), int(total/fileTotalbytes*100)), end="\r")

    if not len(block):
        break

data=b''.join(data_blocks) #had to add b because I was joining bytes not strings
u.close()

with open('test.zip', "wb") as f:
        f.write(data)

推荐答案

要回答您的主要问题,如何制作文本进度条,可以使用类似以下内容的方法:

To answer your main question, how to make a text progress bar, you could use something like the following to give you an idea:

import time

for n in range(1,101):
    hash = ((60*n)//100)
    print("[{}{}] {}%".format('#' * hash, ' ' * (60-hash), n), end="\r")
    time.sleep(0.05)

这将为您提供以下内容:

This would give you the following:

[###########################                                 ] 45%

但是,您的主要问题是,除非您事先已经知道要下载的项目的确切大小,否则没有明显的方法来确定最终将下载多少个字节.如果您控制服务器端,则可以安排在开始之前获取长度.

Your main problem though is that there is no obvious way to determine how many bytes will eventually be downloaded unless you already know the exact size of the item being downloaded beforehand. If you control the server end then you could arrange for the length to be obtained before starting.

不过,您可以至少将 read()行转换为如下所示的内容:

You can though start by at least converting your read() line to something like the following:

u = urllib.request.urlopen(url)

data_blocks = []
total = 0

while True:
    block = fd.read(1024)
    data_blocks.append(block)
    total += len(block)
    print("Downloaded {} bytes".format(total), end="\r")

    if not len(block):
        break

data = "".join(data_blocks)
u.close()

通过这种方式,您可以一次阅读一下,然后可以提供反馈.

By doing it this way, you read it a bit at a time and can then provide feedback.

这篇关于Python 3:如何创建用于下载文件的文本进度栏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 07:48