Replacing line in a text file

Larry Bates larry.bates at websafe.com
Mon Sep 25 16:20:08 CEST 2006


CSUIDL PROGRAMMEr wrote:
> Folks
> I am trying to read a file
> This file has a line containing string  'disable = yes'
> 
> I want to change this line to 'disable = no'
> 
> The concern here is that , i plan to take into account the white spaces
> also.
> 
> I tried copying all file int list and then tried to manipulate that
> list
> 
> But the search is not working
> 
> Any answer
> 
> thanks
> 
Something simple like (not tested):

fp=open(filename, 'r')
data=fp.read()
fp.close()
data='disable = yes'.join(data.split('disable = no',1))
fp=open(filename, 'w')
fp.write(data)
fp.close()

-Larry Bates



More information about the Python-list mailing list