Python watchdog windows wait till copy finishes

前端 未结 7 1072
南笙
南笙 2021-02-05 22:39

I am using the Python watchdog module on a Windows 2012 server to monitor new files appearing on a shared drive. When watchdog notices the new file it kicks off a database resto

7条回答
  •  一个人的身影
    2021-02-05 23:15

    I've tried the check filesize - wait - check again routine many have suggested above but it's not very reliable. To make it work better I've added a check if the file is still locked.

        file_done = False
        file_size = -1
    
        while file_size != os.path.getsize(file_path):
            file_size = os.path.getsize(file_path)
            time.sleep(1)
    
        while not file_done:
            try:
                os.rename(file_path, file_path)
                file_done = True
            except:
                return True
    

提交回复
热议问题