How can I lock a file in Windows?

Flavian Hardcastle deathtospam43423 at altavista.com
Sun Mar 10 14:29:05 EST 2002


Thanx. But will chatters get an IOError if one of them tries to access a 
file while it's already locked? 

"Fredrik Lundh" <fredrik at pythonware.com> wrote in
news:uyqi8.24010$n4.4643918 at newsc.telia.net: 
> 
> did you look under "MS Windows Specific Services" in the
> library reference?
> 
> http://www.python.org/doc/current/lib/module-msvcrt.html
> => "file operations"
> 
> something like this should work:
> 
> def update(filename, new_content):
>     file = open(filename, "r+")
>     # look from current position (0) to end of file
>     msvcrt.locking(file.fileno(), msvcrt.LK_LOCK,
>     os.path.getsize(filename)) file.seek(0)
>     file.write(new_content)
>     file.close() # unlocks the file
> 
> </F>
> 
> <!-- (the eff-bot guide to) the python standard library:
> http://www.pythonware.com/people/fredrik/librarybook.htm
> -->
> 
> 
> 



-- 
"You can tell whether a man is clever by his answers. You can tell whether 
a man is wise by his questions."  Naguib Mahfouz

netvegetable at dingoblue.net.au



More information about the Python-list mailing list