I want to overwrite a file in order to change a certain line. The only code I have come up with is as follows:
import os
def update(fil, argument, line):
z = fil.name
k = fil.readlines()
k[line] = argument
m = file.open("test.txt")
for e in k:
m.write(str(e))
os.remove(fil)
m.name = z
The function goes as follows: I have three parameters:
- the file I want to overwrite
- the argument I want to write
- the line the argument goes at.
Then, I store the name and the lines of the file in two variables, z and k, I say that the (line)th element of k is the argument. After that, I create a new file, write k into it, delete the first file and change the name of the new file to z.
I haven't programmed in Python in a long time, so all help will be greatly welcome!