Fastest Way to Delete a Line from Large File in Python

前端 未结 9 657
轮回少年
轮回少年 2020-11-30 03:58

I am working with a very large (~11GB) text file on a Linux system. I am running it through a program which is checking the file for errors. Once an error is found, I need

9条回答
  •  情话喂你
    2020-11-30 04:57

    I think there was a somewhat similar if not exactly the same type of question asked here. Reading (and writing) line by line is slow, but you can read a bigger chunk into memory at once, go through that line by line skipping lines you don't want, then writing this as a single chunk to a new file. Repeat until done. Finally replace the original file with the new file.

    The thing to watch out for is when you read in a chunk, you need to deal with the last, potentially partial line you read, and prepend that into the next chunk you read.

提交回复
热议问题