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

Micah Mayo astrophels at yahoo.com
Fri Aug 30 23:24:31 CEST 2002


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:
         print '..replacing', orig, 'with',pref
         # will add code to replace eachLine w/ pref later
     else:
         print 'skipping'

and here is the result I get:

skipping
sendmail_enable="YES"
moused_type="NO"

skipping
sendmail_enable="YES"
nfs_reserved_port_only="YES"

skipping
sendmail_enable="YES"
sendmail_enable="YES"

skipping
sendmail_enable="YES"
sshd_enable="YES"

As far as I can tell, they are equal, why isn't it going to the right 
part of the loop?

Also, as you can see, I wish to replace what's in the file with the 
'preg' variable. I was thinking about using string.replace, perhaps 
something like:

file.write(string.replace(eachLine,orig,pref)

Any comments on how to do this, or a better way of running this loop?
Thanks in advance,
Micah




More information about the Python-list mailing list