String find and replace

John Roth newsgroups at jhrothjr.com
Tue Aug 26 19:42:40 EDT 2003


"hokieghal99" <hokiegal99 at hotmail.com> wrote in message
news:bigq2u$p23$1 at solaris.cc.vt.edu...
> 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

what's "str" in line 11?

John Roth
>






More information about the Python-list mailing list