-1

I have a very simple question but I don't find the answer ...

I have a file (data.txt) it contains

A
B
C
D
E
F

I just want to make function to insert line where I want in this file.

Like : Insert('Bouh', 3)

Render :

A
B
C
D
Bouh
E
F

Any idea please ?

1 Answers1

0

UPD: '\n' is already included as the last symbol, so like this

lines = open('demo.txt', 'r').readlines()
position = 3
lines.insert('other line\n', position)
with open('demo.txt', 'w') as f:
    f.write(''.join(lines))
Oleg O
  • 1,005
  • 6
  • 11