Replace line | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Replace line

I want to write a program that gives some integer value. I have a file with a value in the first line. How can I change the value of line (for example to 12). in my code, but this gets a value and I want to go to line 2 and addition m to that number in line 2 but it doesn't work

20th May 2018, 2:09 PM
mohamadamin baniasadi
mohamadamin baniasadi - avatar
1 Answer
+ 1
@jan markus with open('pash.txt', 'r') as f: lines = f.readlines() m = 5 # value you need to add to a line. o = 2 # line number of the line to modify. with open('pash.txt', 'w') as f: for x, line in enumerate(lines): if x == o: line = int(line) + m f.write(line)
20th May 2018, 5:48 PM
mohamadamin baniasadi
mohamadamin baniasadi - avatar