2

I have the following code that's looping through files in a folder and doing a simple search and replace, and then outputs the results to a different folder. What I am noticing is that the replace string seems to be getting applied twice.

For example:

Search string: foo

Replace string: foo bar

Result: foo bar bar

Here is my code. I'm sure the problem is obvious, but I just can't put my finger on it.

def SearchReplace(directory, search, replace, filePattern):
    for path, dirs, files in os.walk(os.path.abspath(directory)):
        for filename in fnmatch.filter(files, filePattern):
            filepath = os.path.join(path, filename)
            outfile = os.path.join(outputdir, filename)
            with open(filepath) as f:
                s = f.read()
            s = s.replace(search, replace)
            with open(outfile, "w") as f:
                f.write(s)
SearchReplace(inputdir, searchstr, replacestr, ext)

NOTE: if I do not output the results to a separate folder, the search/replace performs as expected. Meaning, the code below works fine (modifies input file in same folder):

def SearchReplace(directory, search, replace, filePattern):
    for path, dirs, files in os.walk(os.path.abspath(directory)):
        for filename in fnmatch.filter(files, filePattern):
            filepath = os.path.join(path, filename)
            with open(filepath) as f:
                s = f.read()
            s = s.replace(search, replace)
            with open(filepath, "w") as f:
                f.write(s)
SearchReplace(inputdir, searchstr, replacestr, ext)

However, I need to output the results to a separate folder.

1
  • 1
    Um, what was the original text? If it was foo bar in the beginning... Commented Mar 23, 2012 at 18:12

1 Answer 1

2

The problem is that your output folder is included in the input search pattern, so the replacement is made once on the input file, then again on the output file.

Sign up to request clarification or add additional context in comments.

8 Comments

Expanding on this: If outputdir is "C:\stuff\modified" and inputdir is "C:\stuff", then your code first visits "C:\stuff" writes to "C:\stuff\modified", and then visits "C:\stuff\modified" and writes to "C:\stuff\modified".
Ahh...foiled by the indent. <Python noob hangs head in shame>. Thanks, Mark! :)
One way to avoid this is to add if outputdir==path: continue just below your outer loop.
@keith: I'm not sure I understand. I don't think Mark Byers was addressing indentation. Where is indentation causing this issue?
I just moved (left-indent, or whatever you call it) this code outside the for loop: with open(outfile, "w") as f: See above.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.