Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > String find and replace

Reply
Thread Tools

String find and replace

 
 
hokieghal99
Guest
Posts: n/a
 
      08-26-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

 
Reply With Quote
 
 
 
 
=?ISO-8859-1?Q?Gerhard_H=E4ring?=
Guest
Posts: n/a
 
      08-26-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

 
Reply With Quote
 
 
 
 
John Roth
Guest
Posts: n/a
 
      08-26-2003

"hokieghal99" <(E-Mail Removed)> wrote in message
news:bigq2u$p23$(E-Mail Removed)...
> 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
>



 
Reply With Quote
 
hokiegal99
Guest
Posts: n/a
 
      08-27-2003
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!!!



Geoff Gerrietts wrote:
> Quoting hokieghal99 ((E-Mail Removed)):
>
>>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')

>
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> this string is never bound to a name
> (ie, you never assign str=file(...).read())
>
>> print find
>> if find >=1:
>> replace = string.replace(str, 'THIS', 'THAT')

>
> ^^^
> this name is currently bound to
> the builtin function str()
>
>
>>find_replace(setpath)
>>print " "

>
>
>
> You might consider the fragment below, instead. It's a couple lines
> longer, but safer (your .close() happens exactly when you want it to)
> and probably more readable.
>
> for root, dirs, files in os.walk(setpath):
> fname = files
> for fname in files:
> myfile = file(os.path.join(root,fname), 'rb')
> mystr = myfile.read()
> myfile.close()
> find = string.find(mystr, 'THIS')
> print find
> if find >=1:
> replace = string.replace(mystr, 'THIS', 'THAT')
>
>
> Luck,
> --G.
>



 
Reply With Quote
 
John Roth
Guest
Posts: n/a
 
      08-27-2003

"hokiegal99" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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.


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
>
> Thanks Everyone!!!
>
>
>
> Geoff Gerrietts wrote:
> > Quoting hokieghal99 ((E-Mail Removed)):
> >
> >>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')
> >
> >

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> > this string is never bound to a name
> > (ie, you never assign

str=file(...).read())
> >
> >> print find
> >> if find >=1:
> >> replace = string.replace(str, 'THIS', 'THAT')

> >
> > ^^^
> > this name is currently bound to
> > the builtin function str()
> >
> >
> >>find_replace(setpath)
> >>print " "

> >
> >
> >
> > You might consider the fragment below, instead. It's a couple lines
> > longer, but safer (your .close() happens exactly when you want it to)
> > and probably more readable.
> >
> > for root, dirs, files in os.walk(setpath):
> > fname = files
> > for fname in files:
> > myfile = file(os.path.join(root,fname), 'rb')
> > mystr = myfile.read()
> > myfile.close()
> > find = string.find(mystr, 'THIS')
> > print find
> > if find >=1:
> > replace = string.replace(mystr, 'THIS', 'THAT')
> >
> >
> > Luck,
> > --G.
> >

>
>



 
Reply With Quote
 
hokiegal99
Guest
Posts: n/a
 
      08-27-2003
John Roth wrote:
> 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


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.

 
Reply With Quote
 
John Roth
Guest
Posts: n/a
 
      08-27-2003

"hokiegal99" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> John Roth wrote:
> > 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

>
> 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.


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
>



 
Reply With Quote
 
Erik Max Francis
Guest
Posts: n/a
 
      08-27-2003
hokiegal99 wrote:

> I hate to answer my own post, but I think I understand what I'm doing
> wrong. I'm finding and replacing 'this' with 'that' in the varible
> named
> mystr, not the actual files. So, how would I go about making the
> change
> to the actual files instead of a variable that contains their content?


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 && http://www.velocityreviews.com/forums/(E-Mail Removed) && http://www.alcyone.com/max/
__ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/ \ There never was a good war or a bad peace.
\__/ Benjamin Franklin
 
Reply With Quote
 
hokiegal99
Guest
Posts: n/a
 
      08-27-2003
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.

Erik Max Francis wrote:
> hokiegal99 wrote:
>
>
>>I hate to answer my own post, but I think I understand what I'm doing
>>wrong. I'm finding and replacing 'this' with 'that' in the varible
>>named
>>mystr, not the actual files. So, how would I go about making the
>>change
>>to the actual files instead of a variable that contains their content?

>
>
> Easy:
>
> inputFile = file(filename, 'r')
> data = inputFile.read()
> inputFile.close()
> data = data.replace(this, that)
> outputFile = file(filename, 'w')
> outputFile.write(data)
> outputFile.close()



 
Reply With Quote
 
Thomas =?ISO-8859-15?Q?G=FCttler?=
Guest
Posts: n/a
 
      08-27-2003
hokiegal99 wrote:

> 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.


I once wrote a replace recursive script. Maybe it helps you:
http://www.thomas-guettler.de/script...cursive.py.txt

thomas




 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Replace /n with a XHTML <br /> using string.replace Alun ASP .Net 3 02-18-2008 05:52 AM
how to make replace function replace globally in a string V S Rawat Javascript 5 07-03-2007 08:02 PM
Interactive Find and Replace String Patterns on Multiple Files Xah Lee Java 0 06-14-2006 08:54 PM
Interactive Find and Replace String Patterns on Multiple Files Xah Lee Python 0 06-14-2006 08:54 PM
help with string replace - for doing selective replace Prasad S Javascript 2 08-27-2004 03:22 PM



Advertisments