【发布时间】:2020-04-10 12:46:03
【问题描述】:
我有这样的代码:
chunk_size=512*1024 #512 kb
big_file = open(file, 'rb')
while True:
data = big_file .read(chunk_size)
if not data:
break
如果我只想读取每 10 个项目/元素或每 5 个元素,像这样,我该怎么做?
chunk_size=512*1024 #512 kb
big_file = open(file, 'rb')
counter = 0
while True:
counter +=1
if counter%5!=0:
big_file.next(chunksize) #Just skip it, don't read it...HOW TO DO THIS LINE?
continue #I want to skip the chunk, and in the next loop, read the next chunk.
data = big_file .read(chunk_size)
if not data:
break
在这种情况下,速度对我来说非常重要。我将为数百万个文件执行此操作。我正在做块哈希。
【问题讨论】:
-
我会看看
seek()函数。它应该做你想做的事。只要跟上偏移量。见this link
标签: python python-3.x python-3.6 python-3.7 python-3.8