Script Discussion & Critique

Terry Reedy tjreedy at udel.edu
Wed Aug 27 21:22:55 EDT 2003


"hokiegal99" <hokiegal99 at hotmail.com> wrote in message
news:3F4D4402.3060209 at hotmail.com...
> Is this script written well? Is it a good design? How could it be
made
> better (i.e. write to a file exactly what the scipt did)? I hope to
use
> it to recursively find a replace strings in all types of files
(binary
> and text).

My main answer is your answer to the following: does it operate
correctly? does it produce output within the bounds of what you
consider acceptible?  In particular, does it pass your test case(s)?
and are your test case(s) reasonably representative of the domain of
application?

> Thanks for any ideas, pointers or critique... most of the ideas
behind
> the script came from the list.
>
>
> #Thanks to comp.lang.python
> import os, string
> print " "
> print "******************************************************"
> print "   Three Easy Steps to a Recursive find and Replace   "
> print "******************************************************"

I would lazily print one multiline string.

> print " "
> x = raw_input("1. Enter the string that you'd like to find: ")

I would delete the print and add  '\n' to the front of the prompt

> print " "
> y = raw_input("2. What would you like to replace %s with: " %x)
> print " "
> setpath = raw_input("3. Enter the path where the prgroam should run:
")
> print " "
> for root, dirs, files in os.walk(setpath):
>     fname = files
>     for fname in files:
>        inputFile = file(os.path.join(root,fname), 'r')
>        data = inputFile.read()
>        inputFile.close()
>        search = string.find(data, x)
>        if search >=1:
>           data = data.replace(x, y)
>           outputFile = file(os.path.join(root,fname), 'w')
>           outputFile.write(data)
>           outputFile.close()
>           print "Replacing", x, "with", y, "in", fname
> print " "
> print "**********"
> print "   Done   "
> print "**********"

Terry J. Reedy






More information about the Python-list mailing list