Files
  • main.py
  • data.txt
main.py
1
2
3
4
5
6
7
8
9
10
11
from io import StringIO

buffer = StringIO()

with open('data.txt', 'r') as stream:
    for index, line in enumerate(stream):
        # index == 1 representa a segunda linha do arquivo:
        buffer.write('Novo conteúdo da linha\n' if index == 1 else line)

with open('data.txt', 'w') as stream:
    stream.write(buffer.getvalue())