[Tutor] Checking a file size before moving it

alan.gauld@bt.com alan.gauld@bt.com
Fri Jan 31 08:58:09 2003


> I assume that there is no simple way to tell if a file is 
> being accessed.

OK, I'm at work now with access to my Unix books...

The fcntl function allows you to get a lock. If the file is already 
locked you get that lock back. Within the lock structure you can 
read the user and process IDs. If they aint your own then the file 
is already locked by another user 0- and you know which one!

Itrs a wee bit of processing but you can wrap it as a function 
easily enough and it will be much faster than 10 seconds!

The good news is that there is an fcntl module in Python 
(which uses the FCNTL module to define the needed constants).

F_GETLK is one of these and the function apparently returns a 
flock structure...which I can't see the definition of...

The C definition is a struct:

struct {
   short l_type;
   short l_whence;
   short l_start;
   short l_len;
   short l_sysid;
   short l_pid;
}

>From previous practice I'd guess in Python its a tuple with the 
same set of values in the same order...
  
The bad news is that fcntl only seems to exist in Unix.
For Windows you probably can find something similar in Mark Hammonds 
win32 stuff... But you need to hunt the Win32 docs for that I'm afraid.

Alan g.
Author of the Learn to Program website
http://www.freenetpages.co.uk/hp/alan.gauld/