I have a script that pulls data and writes it into a TXT file, then in the same code I have a For Loop that changes the format by replacing quotes to double quotes and concatenates the result with a text in another new file.
with open ('myfile.txt', 'w') as f:
print(response['animals']['mammals'], file=f)
fout = open("mynewfile.txt", "wt")
f = open('myfile.txt', 'r')
for line in f:
x = str(line).replace("'", '"')
fout.write(f"mammals = {x}")
f.close()
fout.close()
The result is basically that all that is in myfile.txt with quotes i.e ['dog', 'cat'] it is edited and written in mynewfile.txt as mammals = ["dog", "cat"], that is cool. But I also want to manually add some other text to mynewfile.txt and every time I need to update that data and run the script, the data that I enter manually is deleted because of the For Loop.
Is there a way to overwrite just that line without touching the rest of the lines in the file?