Исходный текстовый файл:
line 1
line 2
line 3
line 4
Код:
with open("file.txt", "r") as f:
lines = f.readlines()
with open("file.txt", "w") as out_f:
for i, j in enumerate(lines): # i represents the index of the line starting at 0. j represents the line itself
out_f.write(j.strip("\n") + f"+{i + 1}\n")
Файл после вывода:
line 1+1
line 2+2
line 3+3
line 4+4