我试图对一些文件应用xOr操作,其中一些文件非常大。
基本上我得到了一个文件,并逐字节对其进行异或(或者至少我认为我正在这样做)。当它碰到一个更大的文件(大约70MB)时,我得到一个内存不足的错误,我的脚本崩溃。
我的电脑有16GB的Ram,其中50%以上可用,因此我不会将其与硬件联系起来。在
def xor3(source_file, target_file):
b = bytearray(open(source_file, 'rb').read())
for i in range(len(b)):
b[i] ^= 0x71
open(target_file, 'wb').write(b)
我试图分块读取文件,但似乎我太没经验了,因为输出不是所需的。当然,第一个函数返回我想要的:)
这种手术的合适解决方案是什么?我做错什么了?在
除非我弄错了,否则在第二个示例中,您通过调用}的副本。然后修改}。
bytearray
并将其分配给b
来创建{b
,但返回{b
中的修改对data
本身没有影响。在懒洋洋地迭代大文件。在
使用
seek
函数将文件分块获取,并在每次将其附加到输出文件中相关问题 更多 >
编程相关推荐