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?