Fastest Way to Delete a Line from Large File in Python

前端 未结 9 609
轮回少年
轮回少年 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:33

    def removeLine(filename, lineno):
        in = open(filename)
        out = open(filename + ".new", "w")
        for i, l in enumerate(in, 1):
            if i != lineno:
                out.write(l)
        in.close()
        out.close()
        os.rename(filename + ".new", filename)
    

提交回复
热议问题