So my code looks like this .... but I want to add data always to the end of the document how would I do this
try:
f = open("file.txt", "w")
try:
f.write('blah') # Write a string to a file
f.writelines(lines) # Write a sequence of strings to a file
finally:
f.close()
except IOError:
pass
Open the file using 'a'
(append) instead of 'w'
(write, truncate)
Besides that, you can do the following isntead of the try..finally
block:
with open('file.txt', 'a') as f:
f.write('blah')
f.writelines(lines)
The with
block automatically takes care about closing the file at the end of the block.
open the file with "a" instead of "w"
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