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..finallyblock:The
withblock automatically takes care about closing the file at the end of the block.