0

For example. I wish to delete/add/change lines between 24 and 40 in a txt file. I import txt file in to list by lines. And modify the list then rewrite a new file. here is my code:

def deletetfile(file):
    file = open(file)
    content = file.read()
    lines = content.splitlines()
    print(lines)
    del lines[24:40]
    print(lines)
    with open("testfile2.txt", "w") as f:
        for i in lines:

            f.write(i+'\n')

if __name__ == "__main__":
    deletetfile('testfile.txt')

But I think it gonna runs very slow in a very large file.
Is there a better way to modify txt file's lines in python?

de_jayce
  • 17
  • 4
  • already answerd here https://stackoverflow.com/questions/30971859/python-opening-and-changing-large-text-files – johnashu Apr 22 '20 at 09:23

2 Answers2

2

Beside potential performances issues, reading a whole file in memory can lead to memory errors, so better to avoid it when it's note required. In your case, the simple solution is to read the source file line by line (by iterating over the file), copy lines you want to keep to a new file, then remove the original and rename the new file to the old one. Also, beware of always closing your files (using the with statement being the canonical way to do so):

def delete_lines(file_path, start, end):
    tmp_path = "{}.tmp".format(file_path)
    with open(file_path) as src, open(tmp_path, "w") as dest:
        for lineno, line in enummerate(src):
            if lineno < start or lineno > end:
                dest.write(line)
    os.remove(filepath)
    os.rename(tmp_path, file_path)

if __name__ == "__main__":
    delete_lines('testfile.txt', 24, 40)

nb: untested code so double-check it - but you get the idea ;-)

bruno desthuilliers
  • 75,974
  • 6
  • 88
  • 118
1

Get rid of the for loop and replace it with something like:

f.write(ā€\nā€.join(lines))
rasjani
  • 7,372
  • 4
  • 22
  • 35