Python: Lock directory access under windows

directorylockingpythonwindows

I'd like to be able to lock directory access under windows.
The following code work greatly with file or directory under POSIX system:

def flock(fd, blocking=False, exclusive=False):

    if exclusive:
        flags = fcntl.LOCK_EX
    else:
        flags = fcntl.LOCK_SH
    if not blocking:
        flags |= fcntl.LOCK_NB
    fcntl.flock(fd, flags)

But I only find a way to perform lock access for file, not directory with the following code:

def flock(fd, blocking=False, exclusive=False):

    if blocking:
        flags = msvcrt.LK_NBLCK
    else:
        flags = msvcrt.LK_LOCK
    msvcrt.locking(fd.fileno(), flags, os.path.getsize(fd.name))

Have you got any idea how to improve this code and be able to lock directory access ?

Bertrand

Best Answer

I don't believe it's possible to use flock() on directories in windows. PHPs docs on flock() indicate that it won't even work on FAT32 filesystems.

On the other hand, Windows already tends to not allow you to delete files/directories if any files are still open. This, plus maybe using ACLs intelligently, might get you a 95% equivalent solution.