Script Discussion & Critique

Ganesan R rganesan at myrealbox.com
Thu Aug 28 05:18:35 EDT 2003


>>>>> "hokiegal99" == hokiegal99  <hokiegal99 at hotmail.com> writes:


> 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).

> Thanks for any ideas, pointers or critique... most of the ideas behind
> the script came from the list.

Others have done an excellent job of telling you the problems of critiquing
your code. I'll try to address one point that others haven't covered
already.

> print " "
> print "******************************************************"
> print "   Three Easy Steps to a Recursive find and Replace   "
> print "******************************************************"
> print " "
> x = raw_input("1. Enter the string that you'd like to find: ")
> 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: ")

What you've done is not wrong. But it's simpler to replace all that with 

import sys
x = sys.argv[1]
y = sys.argv[2]
setpath = sys.argv[3]

so that you can run it as "python myscript.py <search> <replace> <path>".
This way you can easily call your script from another program and
makes it much more useful. You can do something like

if len(sys.argv) >= 1:
    x = sys.argv[1]
    y = sys.argv[2]
    setpath = sys.argv[3]
else:
    raw_input(...)
    ...

and get the best of both worlds.

Ganesan
   
-- 
Ganesan R





More information about the Python-list mailing list