troubleshooting Question

Python script to search for and replace text in a file

Avatar of lessthan2
lessthan2 asked on
Python
6 Comments1 Solution939 ViewsLast Modified:
Need help with a simple search and replace script in Python.
In my code, I want to open an HTM file, find "McHugh" and replace it with "REPLACED12345".

#!/usr/bin/python
import string,  os, sys

f = open('sig.txt', 'r')
file_contents = f.readlines()
f.close()

f = open('sig.htm', 'w')
text1 = ("McHugh")
text2 = ("REPLACED12345")

for text1 in 'sig.htm'
        f.write(text1.replace(text1, text2))
f.close()

500 points guys. Just need it to use as a template to save me time. Thanks
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 6 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 6 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros