File monitor reloader

  python, python-3.x

I have some files that are loaded into objects at runtime using a FileHelper class:

file1 = FileHelper("file1.file")
file2 = FileHelper("file2.file")

I’m trying to set up a file monitor so that when a new version of file1.file or file2.file is added, the changes are also updated on file1 and file2 variables

I have created a reload method:

def reload():
    global file1, file2
    if file1.modifiedtime != get_modified_time("file1.file"):
       print("Changing file1"): 
       file1 = FileHelper("file1.file")
    if file2.modifiedtime != get_modified_time("file2.file"):
       print("Changing file2"): 
       file2 = FileHelper("file2.file")

attribute modifiedtime represents the time when the file that is already loaded has been created;

get_modified_time returns the time when the file in the path was modified.

I have created a thread that runs this at a specific interval

def run():
    def job():
        while True:
              print("Checking")
              reload()
              time.sleep(60)
    thread = threading.Thread(target=job)
    thread.start()

Call the script:

run()

When file1 is modified at check I get:
Checking
Changing file1

But the contents of the variables file1 and file2 do not change and are the same as the ones at runtime.

I can’t figure why the content of the variables do not change. I’ve tried a lot of tricks to assign the variables but I got the same results. I’m also a beginer with python, coded a lot more in Java.

Note: file1 and file2 are also imported into another class and I want the changes to propagate there.

Source: Python-3x Questions

LEAVE A COMMENT