- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
如果这看起来是一个基本问题,我深表歉意,在过去的一个小时里,我一直在努力弄清楚如何做到这一点,但没有任何进展。我在 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!
最佳答案
您似乎没有将进度乘以 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 的倍数。
关于Python 2.7 - 为大文件下载输出命令行百分比条时遇到问题(我想要像 : 0%. ..25%...50%...75%...100% 这样的输出)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28701220/
我的函数概念上都返回相同的东西,但结果可以采用不同的形式: function GetThingy() 有四个不同的函数,每个可以返回不同的东西: 0.071(代表增长 7.1% 的 float 值)
这个问题在这里已经有了答案: Int division: Why is the result of 1/3 == 0? (19 个回答) 关闭 4 年前。 有什么方法可以计算(例如)120 的 50
我四处寻找这个,它看起来很简单,但我无法让它工作。 我有一张表格,其中一列需要格式化为百分比。下面是我的代码,但它没有格式化单元格,它只是将它们保留为小数。 我想这是因为 cell ,即使声明为范围,
我刚刚开始使用 WPF。从那以后,我开始关注造型系统。我来自 CSS 背景,我想以百分比设置边距。 当前值以像素为单位
我有一个表,其中每一行都有一个描述字段和一个 bool 值。我正在尝试编写一个查询,我可以在其中按每个相应的描述进行分组,并查看 bool 值为真的次数百分比。 示例表: PID Gen
我从文档中发现,考虑到 orientdb 100% 使用磁盘缓存,它使用的最大大小为 70% 用于读取缓存,30% 用于写入缓存 ( http://orientdb.com/docs/last/plo
有什么方法可以获取 docker 容器内部而不是外部的 cpu 百分比吗?! docker stats DOCKER_ID 显示的百分比正是我需要的,但我需要它作为变量。我需要获取容器本身内部的 cp
我正在尝试计算数据集每列中类别的比例(百分比)。 示例数据: df <- data.frame( "Size" = c("Y","N","N","Y","Y"), "Type" =
我应该使用小数还是 float 在数据库中存储比率?特别是在 SQL2005 中。 最佳答案 这取决于您对准确性的需求。如果您可以容忍来自存储 float 的 IEEE 方法的典型错误,则使用 flo
我正在创建一个游戏,目前必须处理一些math.random问题。 我的Lua能力不是那么强,你觉得怎么样 您能制定一个使用 math.random 和给定百分比的算法吗? 我的意思是这样的函数: fu
如何在SQL中动态计算百分比? 假设您有一个名为 Classes 的下表: ClassSession StudentName -------------------------------
如何通过 jQuery 创建具有百分比的数字掩码输入?我是否让输入仅接受三个数字,并在用户完成输入时在数字后添加百分号(keyup)? 我不使用插件。 示例:1% 或 30% 或 99% 或 100%
我正在尝试构建一个工具,可以突出显示具有最高介数中心性的社交网络节点。我将所有网络节点的测量值计算到字典中,按顺序对字典进行排序,然后仅保留前 3 对。 我希望这个工具是可扩展的,所以我想保留前 10
MYSQL 中的人员如何将一个日期条目和分数的用户百分比与另一个日期条目和分数进行比较,从而有效地返回从一个日期到另一个日期的用户百分比增加情况? 几天来我一直在试图解决这个问题,但我已经没有想法了,
我需要进行查询,结果是百分比。 我现在的查询如下所示: select COUNT(CREATE_WEEKDAY), CREATE_WEEKDAY, COUNT(CREATE
我有一个图像上传功能,其工作原理如下: $('.update-insertimage-form').submit(function() { $(".submit-newupdate-btn").add
我的问题很简单,但我仍然找不到这个问题的答案... 假设我们有两个包含图像的容器。 我们有类似的东西 #containera { width: 50%; height: 50%; backgr
是否可以将 CSS 尺寸指定为除其父元素之外的另一个元素的百分比?例如,我想将 div 的 border-radius 指定为 div 宽度的 10%。但是,border-radius: 10% 在
我正在尝试设置按钮的大小并以百分比进行编辑 但是这个的线性大小是不同的。为什么? 最佳答案 您好,问题出在属性 box-sizing 上.默认为 input type
我将它用于我的页眉,该页眉在一页上下滚动页面中发生变化。我注意到它没有响应,所以我想问你是否知道一种使它响应的方法。就像将 0-690 更改为百分比,以便它可以在移动设备和电视屏幕上使用。 HTML
我是一名优秀的程序员,十分优秀!