editing specific line in text file in python

Solutions on MaxInterview for editing specific line in text file in python by the best coders in the world

showing results for - "editing specific line in text file in python"
Daniela
14 Oct 2016
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 )
Alberto
24 Nov 2018
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
edit specific line txt pythonhow to edit a line in a text file pythonhow to choose a line to edit in a text file in pythonpython edit single line in filehow to edit a specific line in text file in pythonpython edit lines in text filehow to edit a specific line in a file pythonpython how to rewrite each line in the file txtpython change 1 line in fileedit lines of text file pythonhow to edit a line in text file pythonpython modifying specific lines in a filepython edit text file specific linereplace a line in a file pythonhow to go to write to a specific lines in a python filewrite to a file in python on specific lineadd text to specific line in a text file pythonupdating a line in a text file pythonhow to modify a line in a file using pythonpython change one line in filewrite to a specific line pythonpython files edit just a few lineschange a line of a text document pythonpython modify text file line by linehow to rewrite a line in pythonhow to rewrite the content in file from specific line in pythonpython replace lines in filechange line in text file pythonhow to write data to particular line in pythonpython file mode for editing linepython3 write specific lineswrite line in python files at specific linehow to write on a specific line in pythonhow to edit a certain value in a text file in pythonhow to write to a specifit line of a file in pythonpython edit specific line in filepython edit file line by linefile write in a specific line pythonhow to rewrite a line on a particular line number pythonhow to edit a line in pythonhow to edit only a certain line pythonpython how to edit a specific line in filehow to change a specific line in a text file in python edit a specific text line python modifying particular line of a python script via command linepython txt edit whole lineeddit every line in file pythonediting specific line in text file in pythonpython how to edit a specific line of filepython edit line in text filehow to edit a line in text file using pythonhow to replace a specific line in a file pythonedit specific file line in pythonhow to write in a specific line in pythonhow to replace a line in a file using pythonpython how to edit a linehow to edit a line of a file in pythonmodify lines in file pythonediting specific line in text file in pythonaskedhow to just edit a line in txt pythonedit specific line in file pythonhow to edit specific line in python fileedit line in 5c file in pythonhow to write to a specific line in pythonhow to change a line in a file in pythongo to specific line in file pythonhow to read through a file line by line and edit in pythonhow to change a line in a text file pythonpython edit text file at certain linehow to modify a text line in pythonedit a specific line in a file in pythonhow to change a specific word in a specific line in a text file in pythonpython update line in text filepython open file and change linehow to write text into a file in python from a certain linewrite on a specific line pythonhow to write over one line in a text file pythonpython edit line number in a filehow to edit a line in a file in pythonhow to change lines of a text file pythonpython edit line in filepython change line in text fileread files how to change lineshow to have the user edit a line pythonhow to write in a specific line python edit specific line from text with pythonpython write lines to specific line htmlpython file write at a certain linemodify specific line in file pythonpython modify specific line in filepython modifing specific lines in a fileedit specific line in pythonpython edit specific line in file not read full fileedit a specific line in a file in pythowrite in specific line of file pythonhow to change a specific line in a file pythonpython write to file specific lineedit file with python line by lineediting specific line in pythnwrite specific line to existing text file in pythonedit specific line pythonhow to change a specific line in txt file pythonedit a certain line in a file pythonhow to change all lines of a text file using pythonediting a line in a file pyedit specific line in fule pythonpython edit certain line of filehow to write into specific line pythonmodify a line in a file pythonpython edit specific line in enumerate fileedit line in file pythonpython change specific line in text filepython replace specific line in fileedit a line in a file pythonediting specific line in text file in python