I have finished the write files coding lab and I made it while using dictionaries. In the end it can create a file to write into, overwrite the existing file once and then append everything else that needs to go into the file.
I used: https://stackoverflow.com/questions/3900054/python-strip-multiple-characters & https://www.w3schools.com/python/python_file_write.asp
When you open with "a" mode , the write position will always be at the end of the file (an append). There are other permutations of the mode argument for updating (+), truncating (w) and binary (b) mode but starting with just "a" is your best. If you want to seek through the file to find the place where you should insert the line, use 'r+'.
ReplyDeleteThe following code append a text in the existing file:
with open("index.txt", "a") as myfile:
myfile.write("text appended")
source: Append text file in python