PDA

View Full Version : Efficient Folder monitoring.


Benny
09-29-2002, 01:42 AM
Hey all, I'm curious about the best way to achieve something like the following:

Lets say I need a program that constantly monitors an empty directory for a file type. Lets say a jpg image. Then once it detects the file, it does something with it. For the purpose of this lets say it moves it somewhere else.
So I know code such as this would work:


import glob, os

directory1 = "/images"
directory2 = "/images2"

while 1:
listing = glob.glob(os.path.join(directory1, "*.jpg"))
if listing != "":
for item in listing:
os.rename(item, os.path.join(directory2, os.path.basename(item))


However isn't it very bad practise to use "while 1" loops, hence I'd like to know what others think would be a good way to achieve this without the use of a "while 1" loop.

Cheers
Ben.

Strike
09-29-2002, 02:01 AM
Well, you could use select() on the list of file descriptors, I believe.

Or, if FAM (File Alteration Monitor) has some sort of Python bindings somewhere, you could use that.