-1

Am a beginner in Python. I have a set of functions that I want Python to execute whenever the input variable (from csv file) changes. Simply, the program execute the functions, then keep waiting to detect if csv values change, (normally it changes every 2-3 hours), then once change happened, execute again and keep to wait for the next change, and so on.

I would appreciate it if there is a simple understandable way as am beginner in Python and programming.

1 Answers1

0

You can check to see if the file has changed at a given interval like so:

First, store the original version:

f = open('filename','r')
some_var = f.read()
f.close()

Then, set up a loop that will compare it at a given interval (here, ten minutes). I used the sched module, as per this answer: What is the best way to repeatedly execute a function every x seconds?

import sched, time
s = sched.scheduler(time.time, time.sleep)

def checkChange(sc, original, current):
    if original != current:
        # call the function that does your operation here!
    s.enter(600, 1, checkChange, (sc,))

s.enter(600, 1, checkChange, (s,))
s.run()

This code is entirely untested, and probably doesn't work as intended. I just wanted to give you a logical path toward solving your problem. Also, it's likely faster to compare the hashes of the two files' contents, rather than their actual contents. Hope this helped!

Cameron Bond
  • 111
  • 5