How to implement a watchdog like in native python?

Viewed 26

I have a tool which generates some reports as html file. Since there are many and it need to be generated manual organizing them manually will take a lot of time and that's why I tried on making a script which will organize the files automatically with some rules I have applied.

import os
import re
import endwith

filefullname = EndsWith('.html')
allfiles = filefullname.findfile()
report_path = "/home/user/reports/"


while True:
    files = os.listdir("/home/user/")
    if not allfiles:
        continue

    else:

        header = re.match(r"^[^_]+(?=_)", allfiles[0])
        
        if not os.path.exists(report_path + str(header.group())):
            os.system(f"mkdir {report_path + str(header.group())}")

            os.system(f"mv /home/user/*.html reports/{str(header.group())}")
            

        else:
            os.system(f"mv /home/user/*.html reports/{str(header.group())}")

This is the main file which do the automation. and the class is a custom endswith class because the native one returned only boolean types. The thing is it that it runs but the problem is that it requires a restart to finish the job.

Any suggestions?

P.S. This is the class code:

import os

class EndsWith:

    def __init__(self, extension):
    
        self.extension = extension

    def findfile(self):
    
        files = os.listdir("/home/user/")
        file_list = []
        for file in files:
            #print(file)

            if self.extension in file:
                file_list.append(file)
        return file_list
0 Answers
Related