Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python replace line in file

# You can't read and write to a file at the same time so you first need to read
# the contents of the file, save it and then reopen the file after changing the
# values to write the updates to the file. NOTE: when writing to a file it rewrites
# the entire file. This is why saving the original contents of the file is important.
file = []
with open('hello_world.txt', 'r') as f:
    file = f.readlines()

with open('hello_world.txt', 'w') as f:
    file[2] = "This is actually not a test file"
    f.write("".join(file))
Comment

PREVIOUS NEXT
Code Example
Python :: adding roles discord py 
Python :: create qr code in python 
Python :: how to execute a python file from another python file 
Python :: how to setup django ionos hostig 
Python :: python play music 
Python :: finding odd even python 
Python :: how to search in django 
Python :: reading json file 
Python :: python format 001 
Python :: creating numpy array using zeros 
Python :: different states of a button tkinter 
Python :: tkinter entry focus 
Python :: set allowed methods flask 
Python :: list exclude list 
Python :: app is not a registered namespace django 
Python :: How to select parts of a numpy array 
Python :: in python how to use exp 
Python :: code fibonacci python 
Python :: how to unlist a list in python 
Python :: python turtle jupyter notebook 
Python :: create an empty numpy array and append 
Python :: group by, aggregate multiple column -pandas 
Python :: find highest correlation pairs pandas 
Python :: file manage py line 17 from exc django 
Python :: print A to z vy using loop in python 
Python :: rounding values in pandas dataframe 
Python :: cv2 blue color range 
Python :: how to make a discord bot in python 
Python :: python sets 
Python :: replace list 
ADD CONTENT
Topic
Content
Source link
Name
9+3 =