String find and replace

Discussion in 'Python' started by hokieghal99, Aug 27, 2003.

  1. hokieghal99

    hokieghal99 Guest

    import os, string
    print " "
    setpath = raw_input("Enter the path: ")
    def find_replace(setpath):
    for root, dirs, files in os.walk(setpath):
    fname = files
    for fname in files:
    find = string.find(file(os.path.join(root,fname),
    'rb').read(), 'THIS')
    print find
    if find >=1:
    replace = string.replace(str, 'THIS', 'THAT')
    find_replace(setpath)
    print " "

    Why doesn't this work? I get this error:

    Traceback (most recent call last):
    File "html_find_replace.py", line 12, in ?
    find_replace(setpath)
    File "html_find_replace.py", line 11, in find_replace
    replace = string.replace(str, 'THIS', 'THAT')
    File "/usr/local/lib/python2.3/string.py", line 370, in replace
    return s.replace(old, new, maxsplit)
    TypeError: expected a character buffer object
     
    hokieghal99, Aug 27, 2003
    #1
    1. Advertisements

  2. ^^^

    In your app, what do you think 'str' is?

    You haven't defined it, but it still exists. It's the string type, *not*
    a particular string (cos you haven't defined it). That's why you get the
    error below:
    -- Gerhard
     
    =?ISO-8859-1?Q?Gerhard_H=E4ring?=, Aug 27, 2003
    #2
    1. Advertisements

  3. hokieghal99

    John Roth Guest

    what's "str" in line 11?

    John Roth
     
    John Roth, Aug 27, 2003
    #3
  4. hokieghal99

    hokiegal99 Guest

    Thanks for the explanation, I can make it work this way:

    import os, string
    setpath = raw_input("Enter the path: ")
    for root, dirs, files in os.walk(setpath):
    fname = files
    x = 'THIS'
    y = 'THAT'
    for fname in files:
    myfile = file(os.path.join(root,fname), 'r')
    mystr = myfile.read()
    myfile.close()
    search = string.find(mystr, x)
    if search >=1:
    string.replace(mystr, x, y)
    print "Replacing", x, "with", y, "in", fname

    If only I could actually make the change to the files! It works in
    theory, but not in practice ;) Anyone recommend how to actual write the
    change to the file? I'm new to this, so be kind.

    Thanks Everyone!!!
     
    hokiegal99, Aug 27, 2003
    #4
  5. hokieghal99

    John Roth Guest

    Are you trying to rename the file? Look under os - Files and Directories
    for the rename() function. It's 6.1.4 in the 2.2.3 docs.

    John Roth
     
    John Roth, Aug 27, 2003
    #5
  6. hokieghal99

    hokiegal99 Guest

    No, I'm trying to find 'this' in files and replace it with 'that'
    recursively through a directory. It works, but it doesn't actually
    commit the change to the files, it finds 'this' and reports that it
    replaced it with 'that', but when I run the scipt again it reports the
    same files that it just fixed.
     
    hokiegal99, Aug 27, 2003
    #6
  7. hokieghal99

    John Roth Guest

    Oh. You need to open it again for write and write the
    changed string back out. The string doesn't have any
    persistant connection to the file.

    John Roth
     
    John Roth, Aug 27, 2003
    #7
  8. Easy:

    inputFile = file(filename, 'r')
    data = inputFile.read()
    inputFile.close()
    data = data.replace(this, that)
    outputFile = file(filename, 'w')
    outputFile.write(data)
    outputFile.close()
     
    Erik Max Francis, Aug 27, 2003
    #8
  9. hokieghal99

    hokiegal99 Guest

    Easy for you maybe ;)

    That works great. I learn something each time I post to the list. With a
    community like this, Python will be around forever!!! Thanks for the help.
     
    hokiegal99, Aug 27, 2003
    #9
  10. I once wrote a replace recursive script. Maybe it helps you:
    http://www.thomas-guettler.de/scripts/replace-recursive.py.txt

    thomas
     
    Thomas =?ISO-8859-15?Q?G=FCttler?=, Aug 27, 2003
    #10
  11. hokieghal99

    hokiegal99 Guest

    Yes that's very helpful, but a bit too complex for me at this point in
    time. Here is the script that I put together from all of the responses I
    recieved from the list:

    #Thanks to comp.lang.python
    import os, string
    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: ")
    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 "**********"
    print " "
     
    hokiegal99, Aug 27, 2003
    #11
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.