道歉,如果这似乎是一个基本问题,我一直试图弄清楚过去一小时如何做这个没有进展。我在Python中使用这种方法来下载大(10GB +)文件:
def downloadChunks(url, destination):
"""Helper to download large files
the file will be downloaded
in chunks and print out how much remains
modified slightly from: https://gist.github.com/gourneau/1430932
"""
baseFile = os.path.basename(url)
#move the file to a more uniq path
os.umask(0002)
try:
file = os.path.join(destination,baseFile)
req = urllib2.urlopen(url)
total_size = int(req.info().getheader('Content-Length').strip())
downloaded = 0
CHUNK = 256 * 10240 # 256 * 10240 == 2560 KB
with open(file, 'wb') as fp:
while True:
chunk = req.read(CHUNK)
downloaded += len(chunk)
#print math.floor( (downloaded / total_size) * 100 ),
if math.floor( (downloaded / total_size) * 100 ) % 100000000 ==0:
#print "downloaded ="+str(downloaded)+" bytes"
#print "total_size ="+str(total_size)+" bytes ("+str( (float(downloaded) / float (total_size)) )+"%)"
#print ""
if not chunk: break
fp.write(chunk)
except urllib2.HTTPError, e:
print "HTTP Error:",e.code , url
return False
except urllib2.URLError, e:
print "URL Error:",e.reason , url
return False
return file
问题:
当这种方法运行时,我基本上想要输出如下:0%... 25%... 50%... 75%... 100%或10%... 20%......等(由于它们是大文件,我希望粒度可以通过常量调整。)
我知道使用print(message,end ="")但我一直在使用算法。
我尝试过的事情:
你可以看到我过去的一些实验被注释掉了,他们给出了这个输出,对于日志文件显然太长了。
downloaded =2621440 bytes
total_size =611886812 bytes (0.00428419104414%)
downloaded =5242880 bytes
total_size =611886812 bytes (0.00856838208829%)
downloaded =7864320 bytes
total_size =611886812 bytes (0.0128525731324%)
downloaded =10485760 bytes
total_size =611886812 bytes (0.0171367641766%)
downloaded =13107200 bytes
total_size =611886812 bytes (0.0214209552207%)
downloaded =15728640 bytes
total_size =611886812 bytes (0.0257051462649%)
downloaded =18350080 bytes
total_size =611886812 bytes (0.029989337309%)
downloaded =20971520 bytes
total_size =611886812 bytes (0.0342735283531%)
downloaded =23592960 bytes
total_size =611886812 bytes (0.0385577193973%)
downloaded =26214400 bytes
total_size =611886812 bytes (0.0428419104414%)
downloaded =28835840 bytes
total_size =611886812 bytes (0.0471261014856%)
downloaded =31457280 bytes
total_size =611886812 bytes (0.0514102925297%)
downloaded =34078720 bytes
total_size =611886812 bytes (0.0556944835739%)
downloaded =36700160 bytes
total_size =611886812 bytes (0.059978674618%)
只是想知道是否有人可以帮我这样做?非常感谢。
修改
我还写了这个小程序,它输出错误:
程序:
#!/usr/bin/python
import os
import math
scale = 100.0
CHUNK_SIZE=77
FILE_SIZE_TOTAL =611886812
count =0.0
printnum = 25
numIncrememnts = math.floor( scale /printnum)
incrementsize = FILE_SIZE_TOTAL / numIncrememnts
output =0
while True:
if math.floor(count) % math.floor(incrementsize) ==0:
print str(output)+"%"
output= output+printnum
count = count+CHUNK_SIZE
if count >= FILE_SIZE_TOTAL:
break
输出:
khennessy@organization-9758:~/Desktop/VM_deployer$ ./test3.py
0%
khennessy@organization-9758:~/Desktop/VM_deployer$
当CHUNK_SIZE == 1时它可以正常工作。这是输出:
khennessy@organization-9758:~/Desktop/VM_deployer$ ./test3.py
0%
25%
50%
75%
khennessy@organization-9758:~/Desktop/VM_deployer$
编辑 - 我的最终答案:
代码:
def downloadChunks(url, destination):
"""Helper to download large files
the file will be downloaded
in chunks and print out how much remains
modified slightly from: https://gist.github.com/gourneau/1430932
"""
baseFile = os.path.basename(url)
#move the file to a more uniq path
os.umask(0002)
try:
file = os.path.join(destination,baseFile)
req = urllib2.urlopen(url)
total_size = int(req.info().getheader('Content-Length').strip())
downloaded = 0
CHUNK = 256 * 10240 # 256 * 10240 == 2560 KB
val = -1.0
savedval=0.0
with open(file, 'wb') as fp:
while True:
chunk = req.read(CHUNK)
downloaded += len(chunk)
savedval = val
val = 5 * (20 * downloaded / total_size)
if val != savedval:
printInfo(str(val)+"% downloaded. ("+str(downloaded)+" bytes / "+str(total_size)+" bytes)")
if not chunk: break
fp.write(chunk)
except urllib2.HTTPError, e:
print "HTTP Error:",e.code , url
return False
except urllib2.URLError, e:
print "URL Error:",e.reason , url
return False
return file
输出:
[INFO] - 0% downloaded. (2621440 bytes / 611886812 bytes)
[INFO] - 5% downloaded. (31457280 bytes / 611886812 bytes)
[INFO] - 10% downloaded. (62914560 bytes / 611886812 bytes)
感谢JuniorCompressor!
答案 0 :(得分:1)
您似乎没有将进度乘以100来获得百分比。
你也可以像这样对这个百分比进行舍入:
print "total_size =%d bytes (%.0f%%)" % (total_size, 100. * downloaded / total_size)
如果您希望百分比为25的倍数,则可以使用以下代码:
print "total_size =%d bytes (%d%%)" % (total_size, 25 * (4 * downloaded / total_size))
4 * downloaded / total_size
因为整数除法可以有0, 1, 2, 3, 4
个可能值,具体取决于进度所属的范围(0%-25%,25%-50%,50%-75%, 75%-100%)。
同样,您使用10 * (10 * downloaded / total_size)
获得10的倍数。