Python: checking for a new file in a folder

I'm relatively new to python, but I'm trying to create an automated process when my code listens for new file entries in a directory. For example, someone can manually copy a zip file to a specific folder, and I want my code to recognize the file as soon as it is fully copied to the folder. The code can then do some manipulation, but that doesn't matter. I currently have my code that just checks for a new file every 5 seconds, but to me it seems inefficient. Can anyone suggest something more asynchronous?

+4
source share
1 answer

Checking a new file every few seconds is actually not so bad, and in fact it is the only portable way to monitor file system changes. If you are working on Linux, answers to a question related to @sdolan will help you check new files more efficiently, but they will not help you with the other part of your question.

Finding that the file is fully copied is much more difficult than at the beginning. Best of all, when a new file is found, wait until it touches for a while before processing it. The duration of the waiting period is determined experimentally. This is a balancing act: the interval is too short, and you run the risk of working with incomplete files; make it too long and the user will notice a delay between the completion of the copy operation and the processing of the code.

+2
source

All Articles