如果文件太大,无法在一个区块中处理,我如何从文件中读取数据,处理数据并将其重写回文件,而不会在临时文件上浪费内存?
以下代码应该可以工作:
chunksize = 64*1024 #arbitrary number
offset = 0
with open(path, 'r+b') as file:
while True:
file.seek(chunksize*offset) # sets pointer to reading spot
chunk = file.read(chunksize)
if len(chunk) == 0: # checks if EoF
break
elif len(chunk) % 16 != 0: # adds bytes to the chunk if it is the last chunk and size doesnt divide by 16 (if processing text of specific size, my case 16 bytes)
chunk += ' ' * (16 - len(chunk) % 16)
file.seek(chunksize*offset) # returns pointer to beginning of the chunk in order to rewrite the data that was encrypted
file.write(do_something(chunk)) # edits and writes data to file
offset += 1
代码读取数据,返回到块的开头并覆盖它。如果被操纵的数据大于读取的数据,它将不起作用。