gpt4 book ai didi

python - 使用 urlopen 下载的进度条

转载 作者:太空宇宙 更新时间:2023-11-03 15:03:24 25 4
gpt4 key购买 nike

我的要求需要我从 http url 下载一个包并查看其进度。

我写了下面的代码

import subprocess
from urllib import urlopen


class MyClass(object):
'''
classdocs
'''

def url_to_download(self):
url_for_download = "someurl"
file = "somefilename"
print "downloading with urllib"

response = urlopen(url_for_download)
CHUNK = 16 * 1024
with open(file, 'wb') as f:
while True:
chunk = response.read(CHUNK)
cmd = "ls -ltrh" + " " +file + " "+ "|"+ "awk '{print $5}'"
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
(output, err) = p.communicate()
print "the download progress is" + " " + str(output)
if not chunk:
break
f.write(chunk)


if __name__ == "__main__":
download = MyClass()
download.number_of_files()
download.url_to_download()

正如你所看到的,我基本上使用了linux命令来查看下载的进度。有没有什么方法可以通过对代码进行最小的更改来获得下载的水平进度详细信息。提前非常感谢

最佳答案

函数urlretrieve有一个 reporthook 回调 - 即您传递一个 urlretrieve 使用 3 个参数调用的函数。

至于进度条,您可以使用任何progress模块 PyPI。例如,参见tqdm .

关于python - 使用 urlopen 下载的进度条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44870452/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com