Assume I have a text file (named test.txt) that I have wrote 15 lines into it before in my Python script. Now, I want to append some lines to that file. How can I start iteration from line #16 of test.txt and append some new lines to it in Python?
To append at the end of the file, you don't need to "iterate" over it – simply open it in append mode:
with open("my_file", "a") as f:
f.write("another line\n")
Iterating over files can be used to read them, not to write them.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With