Find and replace is appending when run more than once ...

Paul.Scipione at aps.com Paul.Scipione at aps.com
Fri Apr 17 17:01:52 EDT 2009


Hello,

I grabbed some sample code from the web and put together this python script which searches all subdirectories for a specific file type, then replaces a string with a new one.  Out of curiosity I ran it again.  Here's what happens (this also happens for the files I am looking to replace OutputPath=Z:\ with OutputPath=Z:\DXF):

1st run:  replaces OutputPath=Z:\ with OutputPath=Z:\CSV )
2nd run: replaces OutputPath=Z:\CSV with OutputPath=Z:\CSV\CSV
3rd run:  replaces OutputPath=Z:\CSV\CSV with OutputPath=Z:\CSV\CSV\CSV
And so on.

I cant figure out why it's doing this if I am searching a certain string value and replacing it.  I am also perplexed on why it is only adding the "CSV" each time rather than the full string.  What I want it to do is only replace once.  Next time I run it it shouldn't do anything.

import fnmatch,os,sys

mydir = 'C:\\!DOMSExtractor'
findStr = 'OutputPath=Z:\\'

def replaceStringInFile(findStr,replStr,filePath):
    tempName=filePath+'~~~'
    input = open(filePath)
    output = open(tempName,'w')

    for s in input:
        print findStr
        print replStr
        output.write(s.replace(findStr,replStr))
    output.close()
    input.close()
    os.remove(filePath)
    os.rename(tempName,filePath)
    print filePath

def myfun(dummy, dirr, filess):
    for child in filess:
        filePath = dirr+'/'+child
        if '.ini' == os.path.splitext(child)[1] and os.path.isfile(filePath):
            print file(filePath, 'rb').read().find(findStr)
            if file(filePath, 'rb').read().find(findStr) <> -1:
                if fnmatch.fnmatch(dirr+'/'+child, '*BGExtract.ini'):
                    replaceStringInFile(findStr,'OutputPath=Z:\\DXF\\',filePath)
                elif fnmatch.fnmatch(dirr+"/"+child, '*GISExtract.ini'):
                    replaceStringInFile(findStr,'OutputPath=Z:\\CSV\\',filePath)

os.path.walk(mydir, myfun, 3)


Thanks!

Paul J. Scipione
GIS Database Administrator
work: 602-371-7091
cell: 480-980-4721



Email Firewall made the following annotations

---------------------------------------------------------------------
--- NOTICE ---

This message is for the designated recipient only and may contain confidential, privileged or proprietary information.  If you have received it in error, please notify the sender immediately and delete the original and any copy or printout.  Unintended recipients are prohibited from making any other use of this e-mail.  Although we have taken reasonable precautions to ensure no viruses are present in this e-mail, we accept no liability for any loss or damage arising from the use of this e-mail or attachments, or for any delay or errors or omissions in the contents which result from e-mail transmission.

---------------------------------------------------------------------

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20090417/eb4d52ec/attachment.html>


More information about the Python-list mailing list