Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

how to replace first line of a textfile python

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)
 
PREVIOUS NEXT
Tagged: #replace #line #textfile #python
ADD COMMENT
Topic
Name
2+2 =