creating watchfolder in Python, that performs certain operations

泪湿孤枕 提交于 2020-02-04 03:47:29

问题


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://imgur.com/mYhZydf

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://imgur.com/BOaxHzX

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.

来源:https://stackoverflow.com/questions/59857446/creating-watchfolder-in-python-that-performs-certain-operations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!