[Tutor] Recursive search and replace

Daniel Yoo dyoo@hkn.eecs.berkeley.edu
Fri, 13 Apr 2001 03:01:17 -0700 (PDT)


On Thu, 12 Apr 2001, VanL wrote:

> Hello,
> 
> I have written a script to do two things:
>     1. Copy a set of textfiles into every directory in a directory
> tree.
>     2. Edit every htmlfile in the directory tree such that
> "../(../../)textfile.txt" becomes "textfile.txt"
> 
> This script does the first, but not the second.  I successfully open
> each htmlfile, but my regex does not to the matching and
> substitution.  However, when I test the same regex in the
> interpreter, it works perfectly.  Can anyone spot the problem?


You might want to make sure that it's not interfering with case
sensitivity by using the IGNORECASE flag:

    txtfilematch = re.compile(r'txt', re.IGNORECASE)

That's the only think I can think of so far; otherwise, I don't see an
obvious bug in the code.



As a style comment: it might be better to separate the code:

>                                 linelist = open(obj).readlines()
>                                 print "Opened:",
> os.path.join(dirname, obj)
>                                 newfile = open(obj, 'w')
> 
>                                 for line in linelist:
>                                         if re.search(includematch,
> line):
>                                                 print "Found
> include:", line
>                                                 line =
> includematch.sub(r'include file="', line)
>                                                 print "New line:",
> line
>                                                 newfile.write(line)
> 
>                                         else:
>                                                 newfile.write(line)
> 
>                                 newfile.flush()
>                                 newfile.close()

off into a separate function, just to reduce the amount of nested blocks
in the program.  Conceptually, it's doing a large task: it's taking in a
file, fiddling it's contents, and writing it back to disk.  We could call
the function something like fixupFile(f).  By doing this, we can
independently test to see if the string replacement is the bug, or if it's
something else.