Python Assignment Write a python program that replaces text

Python Assignment: Write a python program that replaces text in a file. Your program should prompt the user to enter a filename, an old string, and a new string.

Solution

old=input(\'enter old string :\')

new=input(\'enter new string :\')
f = open(\'newfile.txt\',\'r\')

filedata = f.read()

f.close()

newdata = filedata.replace(old,new)

f = open(\'newfile.txt\',\'w\')

f.write(newdata)

f.close()

Python Assignment: Write a python program that replaces text in a file. Your program should prompt the user to enter a filename, an old string, and a new string

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site