String find and replace

Gerhard Häring gh at ghaering.de
Wed Aug 27 01:32:24 CEST 2003


hokieghal99 wrote:
> 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')
                                         ^^^

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:

> 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

-- Gerhard





More information about the Python-list mailing list