String find and replace

hokieghal99 hokiegal99 at hotmail.com
Tue Aug 26 19:22:06 EDT 2003


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





More information about the Python-list mailing list