How can I check if a line exists in a file?
I’d also like to write the line to the file if it does not exist.
This is my current attempt:
logfile = open('ip.log', 'a+')
while 1:
line = logfile.readline()
#line.replace("\n", "")
print line
if line == str(self.CLIENT_HOST):
print "Line Found Skipping"
else:
logfile.write(str(self.CLIENT_HOST)+"\n")
if not line:
print "EOF Reached"
break
print line
logfile.close()
This is my first quick solution. Very unclean and not yet sophisticated, but should work.