Fastest Way to Delete a Line from Large File in Python

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

    As far as I know, you can't just open a txt file with python and remove a line. You have to make a new file and move everything but that line to it. If you know the specific line, then you would do something like this:

    f = open('in.txt')
    fo = open('out.txt','w')
    
    ind = 1
    for line in f:
        if ind != linenumtoremove:
            fo.write(line)
        ind += 1
    
    f.close()
    fo.close()
    

    You could of course check the contents of the line instead to determine if you want to keep it or not. I also recommend that if you have a whole list of lines to be removed/changed to do all those changes in one pass through the file.

提交回复
热议问题