File locking is impossible in Windows? SOLUTION

Pekka Niiranen pekka.niiranen at wlanmail.com
Wed Dec 22 12:27:42 EST 2004


Hi everybody:

I played with the class Flock and changed the line
win32con.FILE_SHARE_READ|win32con.FILE_SHARE_WRITE,\
to
win32con.FILE_SHARE_READ,\
and now I cannot copy the file over which suits me.

When file is NOT locked I get:
E:\>copy d:\log.txt .
Overwrite .\log.txt? (Yes/No/All): y
         1 file(s) copied.

When file IS locked I get:

E:\>copy d:\log.txt .
The process cannot access the file because it is being used by another 
process.
         0 file(s) copied.

Below is the new script completely. Note that when
upgrading to Python v2.4 I had to change self.highbits
from 0xffff0000 to -0x7fff0000.

-----SCRIPT STARTS----

import win32file
import win32con
import win32security
import pywintypes

class Flock:
    def __init__(self,file):
	self.file=file
	secur_att = win32security.SECURITY_ATTRIBUTES()
	secur_att.Initialize()
	self.highbits=-0x7fff0000
	self.hfile=win32file.CreateFile( self.file,\
	win32con.GENERIC_READ|win32con.GENERIC_WRITE,\
	win32con.FILE_SHARE_READ,\						 	secur_att,\
	win32con.OPEN_ALWAYS,\
	win32con.FILE_ATTRIBUTE_NORMAL , 0)

    def lock(self):
	lock_flags=win32con.LOCKFILE_EXCLUSIVE_LOCK|\
	win32con.LOCKFILE_FAIL_IMMEDIATELY
	self.ov=pywintypes.OVERLAPPED()
	win32file.LockFileEx(self.hfile,lock_flags,0,\
	self.highbits,self.ov)

    def unlock(self):
	win32file.UnlockFileEx(self.hfile,0,\
	self.highbits,self.ov)
	self.hfile.Close()

if __name__ == '__main__':
	import sys
	l=Flock("e:\\\\log.txt")
	print 'calling lock'
	l.lock()
	print "Now locked.  Hit enter to release lock."
	dummy = sys.stdin.readline()

	l.unlock()
	print 'now unlocked'

-----SCRIPT ENDS----

-pekka-


Pekka Niiranen wrote:
> Hi,
> 
> I have used the following example from win32 extensions:
> 
> -----SCRIPT STARTS----
> 
> import win32file
> import win32con
> import win32security
> import pywintypes
> 
> class Flock:
>         def __init__(self,file):
>         self.file=file
>         secur_att = win32security.SECURITY_ATTRIBUTES()
>         secur_att.Initialize()
>         self.highbits=-0x7fff0000
>         self.hfile=win32file.CreateFile( self.file,\
>         win32con.GENERIC_READ|win32con.GENERIC_WRITE,\
>         win32con.FILE_SHARE_READ|win32con.FILE_SHARE_WRITE,\
>         secur_att, win32con.OPEN_ALWAYS,\
>         win32con.FILE_ATTRIBUTE_NORMAL , 0 )
>     def lock(self):
>         lock_flags=win32con.LOCKFILE_EXCLUSIVE_LOCK|\
>         win32con.LOCKFILE_FAIL_IMMEDIATELY
>         self.ov=pywintypes.OVERLAPPED()
>         win32file.LockFileEx(self.hfile,lock_flags,0,\
>         self.highbits,self.ov)
>     def unlock(self):
>         win32file.UnlockFileEx(self.hfile,0,\
>         self.highbits,self.ov)
>         self.hfile.Close()
> 
> if __name__ == '__main__':
>     from time import time, strftime, localtime
>     import sys
>     
>     l=Flock("e:\\\\log.txt")
>     print 'calling lock'
>     l.lock()
>     print "Now locked.  Hit enter to release lock."
>     dummy = sys.stdin.readline()
> 
>     l.unlock()
>     print 'now unlocked'
> 
> -----SCRIPT ENDS----
> 
> If I start one python process from dos window I get message:
> 
> E:\>python lockker.py
> calling lock
> Now locked.  Hit enter to release lock.
> 
> All well, now if
> 
> 1)    I start another Dos -shell and run the same command I get:
> 
>     E:\>python lockker.py
>     calling lock
>     Traceback (most recent call last):
>       File "lockker.py", line 35, in ?
>         l.lock()
>       File "lockker.py", line 23, in lock
>        win32file.LockFileEx(self.hfile,lock_flags,0,\
>        self.highbits,self.ov)
>     pywintypes.error: (33, 'LockFileEx',\
>     'The process cannot access the file because\
>      another process has locked a portion of  the file.')
> 
>     Which is correct.
> 
> 2)    I try to read the contents of the file from Dos -shell, I get:
>     E:\>type log.txt
>     The process cannot access the file because another\
>     process has locked a portion of the file.
>     
>     This is correct.
> 
> 3)    When I open the file into notepad.exe I can edit the screen
>     but not write changes to disk. Correct again!
> 
> 4)    I cannot delete the file from Dos shell or from W2K explorer
>     which is correct.
> 
> 5)    However, I can overwrite the file over with:
>     E:\>copy d:\log.txt log.txt
>         1 file(s) copied.
> 
>     Which is WRONG as is me being able to copy another file over it
>     with W2K explorer too.
> 
> 
> Is there a way around this? How can I stop file being COPIED OVER while
> it is being open? Is this window's feature? Is readlines() operation
> "atomic" enough for me not to worry about these issues?
> 
> My python script modifies set of files from a directory one by one.
> I try to lock them all exclusively for the script
> until all are modified. If one of the files gets overwritten
> by another version (by another process) the script may fail.
> 
> -pekka-
>     
> 
> 
> 
> 
> 
> 



More information about the Python-list mailing list