如何使用python读取、操作和重写文件中的文本

2024-04-25 18:20:21 发布

您现在位置:Python中文网/ 问答频道 /正文

如果临时文件太大,无法在一个块中处理,如何从文件中读取数据、操作数据并将其重写回文件而不浪费临时文件上的内存?你知道吗


Tags: 文件数据内存浪费读取数据
1条回答
网友
1楼 · 发布于 2024-04-25 18:20:21

以下代码应起作用:

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

代码读取数据,返回到块的开头并覆盖它。如果操作的数据大于读取的数据,它将不起作用。他说

相关问题 更多 >