Python script to search for and replace text in a file

lessthan2
lessthan2 used Ask the Experts™
on
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()

Open in new window


500 points guys. Just need it to use as a template to save me time. Thanks
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:
Here is a sample file to run the solution against.
sig.htm
Web Application Programmer
Commented:
Your problem is in the following 2 lines:
for text1 in 'sig.htm'
        f.write(text1.replace(text1, text2))

Open in new window

1. Your loop should run on the list called file_contents (not 'sig.htm')
2. You should use another variable name as the loop control as text1 is already used to store the find value ("McHugh")
3. for statement is missing the ending colon

This should work for you:
#!/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 line in file_contents:
        f.write(line.replace(text1, text2))
f.close()

Open in new window


Alternatively, you can also do this:
#!/usr/bin/python                                                                                                                                                                                

text1 = ("McHugh")
text2 = ("REPLACED12345")

f_in = open('sig.txt', 'r')
f_out = open('sig.htm', 'w')

for line in f_in:
        f_out.write(line.replace(text1, text2))

f_out.close()
f_in.close()

Open in new window

Sandeep KothariProject Lead

Commented:
this should work...
 #!/usr/bin/python
import string,  os, sys

f = open([b]'sig.htm'[/b], 'r')
file_contents = f.read()
f.close()

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

f.write(file_contents.replace(text1, text2))
f.close()

Open in new window

Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

Sandeep KothariProject Lead

Commented:
this should work...the above code got some characters so ignore the above one...

try this..
 #!/usr/bin/python
import string,  os, sys

f = open('sig.htm', 'r')
file_contents = f.read()
f.close()

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

f.write(file_contents.replace(text1, text2))
f.close()

Open in new window

Author

Commented:
This did the trick and taught me a valuable lesson at the same time. Thank you.
Sudaraka WijesingheWeb Application Programmer

Commented:
Glad to help. Thanks for the points.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial