how to add lines to existing file using python
Open the file for 'append' rather than 'write'.
with open('file.txt', 'a') as file:
file.write('input')
If you want to append to the file, open it with 'a'
. If you want to seek through the file to find the place where you should insert the line, use 'r+'
. (docs)
Use 'a'
, 'a'
means append
. Anything written to a file opened with 'a'
attribute is written at the end of the file.
with open('file.txt', 'a') as file:
file.write('input')