I have a Python file that takes the argument -i light1.1 then parses a stats file to see if the current state is 1 or 0. The python file needs to compare the value then either do nothing or change the value and write it back to the same file.
stats file
ligth1=1
light2=0
light3=1
localpub.py file
def main(argv):
  try:
    opts, args = getopt.getopt(argv, "i:h") # Compulsary argument of -i for PHP communication
  except getopt.getoptError:
      print "com.py -i <device.state>"  # Help option print out
      print "or -h for help"
      sys.exit(2)
for opt, arg in opts:
    if opt == "-i":     
        request = arg.split(".")
        with open("stats", "r+") as openFile:   # Temp opens stat file
            for line in openFile:
                if request[0] in line: # Test if requested actuator exist in stats file
                    status = line.rstrip("\n").split("=")   
                    if request[1] == status[1]:
                        print "Same state; Do nothing"
                        break
                    elif int(request[1]) == (1-int(status[1])):
                        print "Diff states! Toggling"
                        newState = status[0] + "=" + request[1] + "\n"
                        print newState
                        openFile.write(newState) # This line deletes the file
if __name__ == "__main__":
    main(sys.argv[1:])      # Runs main function, skips 1st arg as that is the py script name
Any suggestions?
 
     
    