Copy geodatabase(mdb) if it is locked

Tim Golden mail at timgolden.me.uk
Thu Mar 8 11:33:58 EST 2007


Ahmed, Shakir wrote:
> I am trying to copy a geodatabase (.mdb) file from source to destination
> using
> 
> shutil.copyfile(src, dest)
> 
> It is working fine but the main problem when the destination (.mdb) file
> is locked by other users then it's bumped out and not copied over.
> 
> Is there any way to copy the locked .mdb file and overwrite it.


Usual rejoinder: is there any way to do it outside Python? (As far
as I know, there isn't although I'm happy to be wrong). If you haven't
already, try one of the platform-specific lists, since this is
really an OS-issue.

For a Python-ish solution, you might have a retries loop, which
kept trying and backing away for increasing periods of time until
it gave up or succeeded. Rough-and-ready and completely untested:

<code>
import shutil
import time

src = "c:/temp/temp.mdb"
dest = "c:/temp/temp2.mdb"

n_retries = 5
base_delay = 2

n_retry = 0
while n_retry < n_retries:
   try:
     shutil.copyfile (src, dest)
   except IOError:
     time.sleep (2 ** n_retry)
     n_retry += 1
     continue
   else:
     print "succeeded after", n_retry, "retries"
     break
else:
   print "failed after", n_retry, "retries"

</code>

TJG



More information about the Python-list mailing list