File handling 16: Replace a word in file

# Replace a word in a file

fs = open("ipt.txt", "r+")
old_str = input("Enter string to replace : ").strip()
new_str = input("Enter new string : ").strip()

Line = fs.read()
Line = Line.replace(old_str, new_str)

fs.seek(0,0)
fs.truncate()
fs.write(Line)

fs.seek(0,0)
Line = fs.read()
print(Line)

fs.close()

No comments:

Post a Comment

Don't be a silent reader...
Leave your comments...

Anu