Answers for "Editing specific line in text file in PythonAsked"

4

how to edit a specific line in text file in python

# with is like your try .. finally block in this case
with open('stats.txt', 'r') as file:
    # read a list of lines into data
    data = file.readlines()

print data
print "Your name: " + data[0]

# now change the 2nd line, note that you have to add a newline
data[1] = 'Magen'

# and write everything back
with open('stats.txt', 'w') as file:
    file.writelines( data )
Posted by: Guest on September-08-2020
-1

editing specific line in text file in python

# with is like your try .. finally block in this case
with open('stats.txt', 'r') as file:
    # read a list of lines into data
    data = file.readlines()

print data
print "Your name: " + data[0]

# now change the 2nd line, note that you have to add a newline
data[1] = 'Magen'

# and write everything back
with open('stats.txt', 'w') as file:
    file.writelines( data )
Posted by: Guest on October-02-2020

Code answers related to "Editing specific line in text file in PythonAsked"

Python Answers by Framework

Browse Popular Code Answers by Language