From: Chris Rebert on
On Tue, May 4, 2010 at 2:37 PM, <python(a)bdurham.com> wrote:
> Is there a way to exclusively lock a file to prevent other processes from
> reading it while we have it open?
>
> I need to cache some overflow data to disk in a temp file and I want to make
> sure no other processes can read the contents of this file while I'm using
> it.
>
> I tried the following using an 'append binary' mode, but I can use NotePad
> to read the file while I'm using it:
>
> fd = open( r'a-temp-file.dat', 'ab' )
>
> My environment is Python 2.6.4 (32-bit) under Windows, but I'm looking for a
> cross-platform solution if that's possible.

I remember this recipe (or a variant) from the dead-tree Python Cookbook:
http://code.activestate.com/recipes/65203-portalocker-cross-platform-posixnt-api-for-flock-s/

Cheers,
Chris
--
http://blog.rebertia.com