Searching and Replacing Text in a File
Credit: Jeff Bauer
Problem
You need to change one string into another throughout a file.
Solution
String substitution is most simply
performed by the
replace
method of string objects. The work here is to support reading from
the specified file (or standard input) and writing to the specified
file (or standard output):
#!/usr/bin/env python
import os, sys
nargs = len(sys.argv)
if not 3 <= nargs <= 5:
print "usage: %s search_text replace_text [infile [outfile]]" % \
os.path.basename(sys.argv[0])
else:
stext = sys.argv[1]
rtext = sys.argv[2]
input = sys.stdin
output = sys.stdout
if nargs > 3:
input = open(sys.argv[3])
if nargs > 4:
output = open(sys.argv[4], 'w')
for s in input.xreadlines( ):
output.write(s.replace(stext, rtext))
output.close( )
input.close( )Discussion
This recipe is really simple, but that’s what beautiful about it—why do complicated stuff when simple stuff suffices? The recipe is a simple main script, as indicated by the leading “shebang” line. The script looks at its arguments to determine the search text, the replacement text, the input file (defaulting to standard input), and the output file (defaulting to standard output). Then, it loops over each line of the input file, writing to the output file a copy of the line with the substitution performed on it. That’s all! For accuracy, it closes both files at the end.
As long as it fits comfortably in memory in two copies (one before and one after the replacement, since strings ...