Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

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] = 'Mage
'

# and write everything back
with open('stats.txt', 'w') as file:
    file.writelines( data )
Comment

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] = 'Mage
'

# and write everything back
with open('stats.txt', 'w') as file:
    file.writelines( data )
Comment

PREVIOUS NEXT
Code Example
Python :: python linked list insert 
Python :: pandas qcut 
Python :: python left string 
Python :: Can there be an if statement inside an if statement python 
Python :: waitkey in python 
Python :: TypeError: cannot unpack non-iterable float object evaluate 
Python :: pygame screen 
Python :: python sched 
Python :: python excel sheet import 
Python :: printing in python 
Python :: get the largest of 2 strings python 
Python :: python program to find sqaure root of the number 
Python :: code a gui 
Python :: lowering the time.countdown python 
Python :: findout age in python 
Python :: load text file line in listbox python tkinter site:stackoverflow.com 
Python :: required depend filed odoo 
Python :: Installez django-cruds-adminlte 
Python :: split a column into two columns pandas 
Shell :: how to check laptop serial number in ubuntu 
Shell :: emu8086 registration key 
Shell :: npm list global packages 
Shell :: remove identifier files wsl2 
Shell :: how to kill a process on a port? 
Shell :: how to add docker to sudo group 
Shell :: pip install urllib 
Shell :: android gradle signing report 
Shell :: linux sort folders by size 
Shell :: beautifulsoup4 install 
Shell :: install putty on ubuntu 
ADD CONTENT
Topic
Content
Source link
Name
2+6 =