Answers for "python change first line of file"

1

get first line of file python

with open('myfile.txt') as f:
    first_line = f.readline()
Posted by: Guest on November-19-2020
0

how to replace first line of a textfile python

with open("test.txt") as f:
    lines = f.readlines()

lines # ['This is the first line.n', 'This is the second line.n']

lines[0] = "This is the line that's replaced.n"

lines # ["This is the line that's replaced.n", 'This is the second line.n']

with open("test.txt", "w") as f:
    f.writelines(lines)
Posted by: Guest on November-17-2019

Code answers related to "python change first line of file"

Python Answers by Framework

Browse Popular Code Answers by Language