我的需求需要我從http url下載一個包並查看其相關進度。使用urlopen下載進度條
我在下面寫
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命令,以查看download.Is有以最小的改動任何代碼方式的進展情況,我可以有一個水平進度下載的詳細信息。 非常感謝提前
感謝alot.i使它與tqdm一起工作 –