Here’s a script that inserts data after a line.it works when data is given directly. But i want it to take data from a text file and then insert that data.
can someone tell me where i went wrong and how to correct that or do this in a better manner with explanation( I’m new to python, and i have to apply this method on other files as well).
Thanks
I tried
#new line to be inserted in sample.txt
with open("sample.txt", "r") as f:
contents = f.readlines()
#take data to be inserted from calinks.txt
with open("calinks.txt", "r") as g:
contentsg = [ ]
contentsg = g.readlines()
contents.insert(2, contentsg)
with open("sample.txt", "w") as f:
contents = "".join(contents)
f.write(contents)
Error: contents = “”.join(contents)
^^^^^^^^^^^^^^^^^
TypeError: sequence item 1: expected str instance, list found
xmsk is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.