Answers for "replace a line with another file using python"

1

python replace text in file

filename = "sample1.txt"
# SAMPLE1.TXT
# Hello World!
# I am a human.

with open(filename, 'r+') as f:
    text = f.read()
    text = re.sub('human', 'cat', text)
    f.seek(0)
    f.write(text)
    f.truncate()

# SAMPLE1.TXT
# Hello World!
# I am a cat.
Posted by: Guest on November-07-2020

Code answers related to "replace a line with another file using python"

Python Answers by Framework

Browse Popular Code Answers by Language