Welcome, guest | Sign In | My Account | Store | Cart

A simple text search and replace program.

Python, 24 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#!/usr/bin/env python
import os, sys
usage = "usage: %s search_text replace_text [infile [outfile]]" %         os.path.basename(sys.argv[0])

if len(sys.argv) < 3:
    print usage
else:
    stext = sys.argv[1]
    rtext = sys.argv[2]
    input = sys.stdin
    output = sys.stdout
    if len(sys.argv) > 3:
        input = open(sys.argv[3])
    if len(sys.argv) > 4:
        output = open(sys.argv[4], 'w')
    for s in input.xreadlines():
        output.write(s.replace(stext, rtext))


  # For older versions of Python (1.5.2 and earlier) import
  # the string module and replace the last two lines with:
  #
  # for s in input.readlines():
  #     output.write(string.replace(s, stext, rtext))

Perhaps this is too simple to be included as a cookbook entry, but it's a frequently asked question. Maybe teachers will stop assigning it as a homework problem. ;-)

5 comments

Adam Krieg 22 years, 3 months ago  # | flag

Slight Mods. This code as is will not work. The main problem is that the output file object is not closed, so nothing actually gets written to the file. Also xreadlines is not a method of the file object (at least not in Jython, where I tested this). Anyway, give this one a go:

#!/usr/bin/env python
import os, sys, xreadlines
usage = "usage: %s search_text replace_text [infile [outfile]]" %         os.path.basename(sys.argv[0])

if len(sys.argv) &lt; 3:
        print usage
else:
        stext = sys.argv[1]
        rtext = sys.argv[2]
        input = sys.stdin
        output = sys.stdout
        print "There are %s args " %len(sys.argv)
        if len(sys.argv) &gt; 3:
            input = open(sys.argv[3])
        if len(sys.argv) &gt; 4:
            output = open(sys.argv[4], 'w')
        print "output = ",output
        for s in xreadlines.xreadlines(input):
        print "s= ",s
            output.write(s.replace(stext, rtext))
        if len(sys.argv)&gt; 3:
        input.close()
        if len(sys.argv) &gt; 4:
            output.close()
Jani Peki 18 years, 4 months ago  # | flag

xreadlines is deprecated. Since xreadlines is deprecated the solution should look like this:

#!/usr/bin/python
import os, sys
usage = "usage: %s search_text replace_text [infile [outfile]]" %         os.path.basename(sys.argv[0])

if len(sys.argv) &lt; 3:
       print usage

else:
        stext = sys.argv[1]
        rtext = sys.argv[2]
        input = sys.stdin
        output = sys.stdout
        print "There are %s args " %len(sys.argv)

        if len(sys.argv) > 3:
                input = open(sys.argv[3])

        if len(sys.argv) > 4:
                output = open(sys.argv[4], 'w')

        for s in input:
                output.write(s.replace(stext, rtext))

        if len(sys.argv)> 3:
            input.close()
            if len(sys.argv) > 4:
                    output.close()
Alex Manus 16 years, 11 months ago  # | flag

None of these work. Actually, all of these examples are wrong, s is not defined anywhere in the script and the replace method requires 3 arguments.

Daniel Eggleston 16 years, 7 months ago  # | flag

Actually. You are wrong... s is declared in the for loop where it is used, and the third argument to replace is optional.

Shawn Clark 14 years, 8 months ago  # | flag

great recipe, it has been years since I did Python so I appreciate it!