monitoring a directory

Michael Geary Mike at DeleteThis.Geary.com
Fri Mar 26 12:25:30 EST 2004


Tim Golden wrote:

> There are two methods that I know of (apart from the simple
> approach you describe). One is described in this recipe:
>
> http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/156178
>
> This is simple to use, but falls down a bit if you have more than a few
> files in the directory, since it doesn't know what's changed. It's fine if
> you're just happy to rescan, say for a file-manager app.
>
> The other method, which is a bit more sophisticated, uses the
> recently-added-to-pywin32 win32file.ReadDirectoryChangesW
> functionality.

> The other method, which is a bit more sophisticated, uses the
> recently-added-to-pywin32 win32file.ReadDirectoryChangesW
> functionality.

Note that ReadDirectoryChangesW is available only on the NT platforms (XP,
2000, NT4, NT3.51 SP3). If you need portability to Win95/98/Me, you'll need
to fall back to FindFirstChangeNotification as the cookbook recipe uses.

Does win32file.ReadDirectoryChangesW provide a way to tell if it's available
or not? I'd hope that win32file would simply not define
ReadDirectoryChangesW on Win9x systems, so you could code like this:

if win32file.ReadDirectoryChangesW:
    useReadDirectoryChangesW()
else:
    useFindFirstChangeNotification()

I don't see any mention of that in the docs, so you probably have to do a
version check instead:

import win32api, win32con

def isWindowsNT():
    return( win32api.GetVersionEx()[3] ==
            win32con.VER_PLATFORM_WIN32_NT )

if isWindowsNT():
    useReadDirectoryChangesW()
else:
    useFindFirstChangeNotification()

(To really do that right, you'd need to check the GetVersionEx return value
more thoroughly to test for NT versions prior to 3.51 SP3, which don't have
ReadDirectoryChangesW.)

-Mike





More information about the Python-list mailing list