2

Currently I'm working on project which aims on creating watchfolder in Python that would perform certain operations. I read about watchdog library and I'm trying to implement it right now. My code is based on: Using watchdog of python to monitoring afp shared folder from linux

Here is structure of folders and subfolders on which operations have to be performed. https://i.stack.imgur.com/c80Mu.jpg

The main folder watchfolder contain subfolders subfolX observed recursively. At the beginning, There is only watchfolder containing empty subfolders. I have another program that moves special directories VIDX to specific subfolders. Now, three operations are done:

settings_extractor() is executed. This function open file adi.xml and basing on it create new file settings.txt. mediainfo_extractor() is executed. This function returns specific information about the VIDX.mp4 file. This information is then used to create folderName variable. Basing on folderName the whole folder VIDX with contents (VIDX.mp4, settings.txt, adi.xml) is moved to another directory based on folderName variable.

Exemplary process of folder VID2 being moved to subfol3 is here: https://i.stack.imgur.com/lIT04.jpg

However I encountered few problems. I don't know how should I properly set the operations order so it will result in desirable effect. I cannot understand 'recursiveness' completely, because program is performing some operations with clearing local variables. When I run program, it analyses *.mp4 file first, creates folderName variable, however new event triggers memory clear and then script doesn't know where should it move files.

The basic watchfolder code is the same as in first link, however there is version on pastebin: https://pastebin.com/Qh4mvgU1

Here is my code responsible for operations:

    if(event.event_type == 'created'):
        filename = path_leaf(event.src_path)            
        extension = os.path.splitext(event.src_path)[-1].lower()

        if filename == "adi.xml":
            adi_to_settings.settings_extractor(event.src_path)
            shouldMove = True

        if extension == ".mp4":
            #<PART OF CODE RESPONSIBLE FOR CREATING folderName>
            folderName = folderName.upper()
            folderThatWillBeMoved = str(Path(event.src_path).parent)


        if(len(os.listdir(r"C:\Users\user\WFS\watchfolder\subfol3\{}".format("folderThatWilLBeMoved))) >= 3 & shouldMove == True):
            moveAllDirsAndFilesinDir(r'C:\Users\user\WFS\watchfolder\{}'.format("folderThatWillBeMoved"),r'C:\Users\user\WFS\tempfolder\{}'.format(folderName))
            shouldMove = False

Summarizing, the main problem I have is to have control which operation on file will be performed first, or even better, which file is analysed first. I tried to explain everything as brief and as clean as I could. I'd thankful for any hint bringing me closer to solution, how should I modify code in order to perform operations in a clean manner.

  • One problem you mention is: **'because program is performing some operations with clearing local variables'**. [This post](https://stackoverflow.com/questions/12570856/how-can-i-give-variables-to-the-event-handler) provides two methods to maintain variables with the simplest being to use global variables. – DarrylG Jan 24 '20 at 13:23
  • As a follow up to my comment, assuming your handler is a class then class variables could be used which would be better than globals in this case. [Class Variables](https://www.digitalocean.com/community/tutorials/understanding-class-and-instance-variables-in-python-3) – DarrylG Jan 24 '20 at 13:43

0 Answers0