请问Python有办法同时读写文件么?

比如说,我这里有个100g的文件(电脑内存不足100g)我想用字节形式打开。然后给最前面十个16进制每个加一。然后再把这个100g的文件输出。现在的问题是我必须读取一段,然后再输出一段,效率太低。有没有别的什么办法可以让读取和输出同时进行?实现近似甚至超过复制粘贴的速度。

img


初步思路是这样,但是还有很多问题,比如如何继续读取第二个1024而不是第一个,old的序号等。
或者用别的什么自动内存管理

双线程进行呗,一个读一个写,弄个list进行缓存数据

import threading, time
readnow = []
STOP = False
def out(): #写入函数
   global STOP
   while not STOP:
      if not len(readnow):
         data = readnow.pop(0)
         #处理然后写入数据
      else:
         time.sleep(0.01) #防止高cpu占用
def in_(): #读取函数
  with open("file", 'rb') as f:
    while not STOP:
        data = f.read(1024)
        if data == b'': #读完了
            return
        readnow.append(data) if len(readnow) >= xxx else None #1024b一个块,当list内数据大于多少组的时候停止读取等待out线程输出完毕
        time.sleep(0.01)
        

threading.Thread(target=out).start()
threading.Thread(target=in_).start()

Python 实现大文件读写,可以参考这个:
https://blog.csdn.net/qq_19394437/article/details/115557212