When the code is run, it executes correctly and no errors appear, and the results are expected, but the file is not "overwritten". What is the solution?
Rewrite the file even though the code is correct and there are no errors!
space_indecies = [2, 5, 8]
with open("data.txt", "r") as file:
lines = file.read().split("\n")
newlines = []
for line in lines:
line = line.rstrip()
for n, i in enumerate(space_indecies):
line = line[:i + n] + ' ' + line[n + i:]
newlines.append(line)
with open("data.txt", "w") as newfile: # Maybe wrong with this sentence
newfile.write("\n".join(newlines))
expected result : If there is an error in the writing statement inside the file, please modify it correctly, with the same results.