how to edit a specific line in text file in python

Solutions on MaxInterview for how to edit a specific line in text file in python by the best coders in the world

showing results for - "how to edit a specific line in text file in python"
Benjamin
09 May 2017
1# with is like your try .. finally block in this case
2with open('stats.txt', 'r') as file:
3    # read a list of lines into data
4    data = file.readlines()
5
6print data
7print "Your name: " + data[0]
8
9# now change the 2nd line, note that you have to add a newline
10data[1] = 'Mage\n'
11
12# and write everything back
13with open('stats.txt', 'w') as file:
14    file.writelines( data )
Giuseppe
30 Sep 2020
1# with is like your try .. finally block in this case
2with open('stats.txt', 'r') as file:
3    # read a list of lines into data
4    data = file.readlines()
5
6print data
7print "Your name: " + data[0]
8
9# now change the 2nd line, note that you have to add a newline
10data[1] = 'Mage\n'
11
12# and write everything back
13with open('stats.txt', 'w') as file:
14    file.writelines( data )
15
queries leading to this page
how to edit a specific line in a file pythonhow to write to a specifit line of a file in pythonpython edit line in text filehow to edit specific line in python filehow to change a specific line in txt file pythonpython file write at a certain lineedit specific line from text with pythonedit line in 5c file in pythonhow to edit only a certain line pythonhow to write data to particular line in pythonchange line in text file pythonhow to change a specific line in a text file in python editing specific line in text file in pythonaskedpython modify text file line by lineedit specific line in pythonhow to edit a line of a file in pythonpython how to edit a specific line in fileedit lines of text file pythonpython change line in text filehow to just edit a line in txt pythonpython edit line number in a fileedit specific line txt pythonedit specific line pythonpython edit lines in text fileediting specific line in text file in pythonpython open file and change linepython modifing specific lines in a filepython update line in text filepython edit text file at certain linehow to edit a line in text file pythonpython edit specific line in file not read full filehow to read through a file line by line and edit in pythonpython write lines to specific line htmlmodify specific line in file pythonhow to edit a line in pythonchange a line of a text document pythonwrite in specific line of file pythonpython edit text file specific lineedit a line in a file pythonmodify a line in a file pythonmodify lines in file pythonedit specific line in fule pythonpython edit certain line of filepython replace lines in filepython files edit just a few linespython edit line in filehow to edit a line in a file in pythonedit specific file line in pythonedit a specific text line python python edit single line in filemodifying particular line of a python script via command linewrite on a specific line pythonhow to go to write to a specific lines in a python filepython change one line in fileupdating a line in a text file pythonediting specific line in pythnpython file mode for editing lineediting a line in a file pyhow to write into specific line pythonpython change 1 line in filepython3 write specific lineshow to edit a line in text file using pythongo to specific line in file pythonhow to write in a specific line in pythonhow to modify a line in a file using pythonwrite to a file in python on specific linepython modifying specific lines in a fileedit file with python line by linepython how to edit a linehow to edit a specific line in text file in pythonread files how to change lineshow to change a line in a file in pythonhow to rewrite a line in pythonhow to change a specific word in a specific line in a text file in pythonhow to replace a line in a file using pythonedit a specific line in a file in pythofile write in a specific line pythonpython txt edit whole lineedit a certain line in a file pythonhow to write in a specific line python how to have the user edit a line pythonpython modify specific line in filehow to change lines of a text file pythonhow to edit a line in a text file pythonhow to rewrite the content in file from specific line in pythonpython how to edit a specific line of filepython edit specific line in enumerate filehow to write text into a file in python from a certain lineedit line in file pythonhow to write on a specific line in pythoneddit every line in file pythonhow to write over one line in a text file pythonhow to choose a line to edit in a text file in pythonadd text to specific line in a text file pythonedit specific line in file pythonhow to replace a specific line in a file pythonpython edit specific line in filehow to rewrite a line on a particular line number pythonhow to change a line in a text file pythonhow to write to a specific line in pythonpython how to rewrite each line in the file txtedit a specific line in a file in pythonhow to edit a certain value in a text file in pythonpython edit file line by linewrite to a specific line pythonhow to modify a text line in pythonhow to change all lines of a text file using pythonhow to change a specific line in a file pythonpython replace specific line in filewrite line in python files at specific linepython write to file specific linehow to edit a specific line in text file in python