with open("test.txt") as f:
lines = f.readlines()
lines # ['This is the first line.
', 'This is the second line.
']
lines[0] = "This is the line that's replaced.
"
lines # ["This is the line that's replaced.
", 'This is the second line.
']
with open("test.txt", "w") as f:
f.writelines(lines)