Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Checking if new file is in folder [duplicate]

Tags:

python

file-io

I am relatively new to python, but I am trying to create an automated process where my code will listen for new file entries in a directory. For example, someone can manually copy a zip file into a particular folder, and I want my code to recognize the file once it has completely been copied into the folder. The code can then do some manipulations, but that is irrelevant. I currently have my code just checking for a new file every 5 seconds, but this seems inefficient to me. Can someone suggest something that is more asynchronous?

like image 877
user411423 Avatar asked Nov 06 '22 10:11

user411423


1 Answers

Checking for new file every several seconds is actually not that bad an approach — in fact, it is the only really portable way to monitor for filesystem changes. If you're running on Linux, answers in the question linked by @sdolan will help you check for new files more efficiently, but they won't help you with the other part of your question.

Detecting that the file has been copied completely is much more difficult than it first appears. Your best best is, when a new file is detected, wait until it hasn't been touched for a while before processing it. The length of the wait period is best determined experimentally. It's a balancing act: make the interval too short, and you're risking operating on incomplete files; make it too long, and the user will notice a delay between the copy operation finishing and your code processing it.

like image 171
user4815162342 Avatar answered Nov 11 '22 04:11

user4815162342