Why doesn't this work? - comparing read lines from a file - string.replace question also.

holger krekel pyth at devel.trillke.net
Fri Aug 30 23:33:12 CEST 2002


Micah Mayo wrote:
> I have this little bit of code, that reads the standard rc.conf file 
> that is installed with freebsd. I don't understand why a simple if 
> statement is not working, please help!
> 
> #!/usr/local/bin/python
> import string
> 
> orig = 'sendmail_enable="YES"'
> pref = 'sendmail_enable="NONE"'
> 
> for eachLine in open('rc.conf','r+'):
>      print orig,'\n',eachLine
>      if eachLine == orig:

eachLine has a trailing newline while your 'orig' string has not.  try

    if eachLine.find(orig)!=-1:

regards,

    holger




More information about the Python-list mailing list