[Tutor] Find multiple lines in a file
billburns@pennswoods.net
billburns at pennswoods.net
Wed Nov 9 18:34:55 CET 2005
[Kent wrote]
> > OK, none of my suggestions gets you closer to this...the simplest way is
> > if you can read the whole file into memory. Then you can just replace
> > the strings in place and write it out again. For example:
> >
> > oldPolicies = '''<<
> > /Policies <<
> > /PageSize 3
> > >>
> >
> >>> setpagedevice'''
> >
> >
> > newPolicies = 'something completely different'
> >
> > f = open(filename)
> > data = f.read()
> > f.close()
> > data.replace(oldPolicies, newPolicies)
> > f = open(filename, 'w')
> > f.write(data)
> > f.close()
Hi Kent,
I just tested the following code and it works like a champ :-)
filename = r'C:\TEST.ps'
old = '''<<
/Policies <<
/PageSize 3
>>
>> setpagedevice'''
new = '''<<
/EndPage
{ exch pop
0 eq
{ gsave
initmatrix
save
850 1250 moveto 45 rotate 0 setgray
/Helvetica 25 selectfont (CAUTION - DO NOT SCALE!) true charpath .2
setlinewidth stroke
restore
true
}
{ false }
ifelse
} bind
>> setpagedevice'''
f = open(filename)
data = f.read()
f.close()
newData = data.replace(old, new)
f = open(filename, 'w')
f.write(newData)
f.close()
Thanks again for your help!!
Bill
More information about the Tutor
mailing list