Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am having problems with lock files while deleting and copying geodatabases for backup purposes.

print "Now backing up your Geodatabase"
distutils.dir_util.remove_tree (r"\\Msukserver\gis\geodatabase BACKUP\EVERYONE\Mining Features (MATTHEW).gdb", 0, 0) # deletes 'everyone' backup copy
distutils.dir_util.copy_tree(r"\\Msukserver\gis\geodatabase BACKUP\MATT\Mining Features (MATTHEW).gdb", r"\\Msukserver\gis\geodatabase BACKUP\EVERYONE\Mining Features (MATTHEW).gdb") # backup the last backup to 'everyone'
print "old backup copied"
distutils.dir_util.remove_tree(r"\\Msukserver\gis\geodatabase BACKUP\MATT\Mining Features (MATTHEW).gdb", 0, 0) # deletes last backup
print "old backup deleted"
distutils.dir_util.copy_tree(r"C:\GIS Home\Mining Features (MATTHEW).gdb", r"\\Msukserver\gis\geodatabase BACKUP\MATT\Mining Features (MATTHEW).gdb") # copy mining features to create new backup
print "::::::::::::::::::::::Backup Complete::::::::::::::::::::::"
raw_input("Push Return key to close...")

I am getting a 'permission denied' error on the '.lock' files. Is there a way of copying/deleting all the file except those with a .lock extension? or a way to continue/force with errors? I am aware that I am currently copying/deleting the entire directory and may need to change my code to copy/delete files only.

share|improve this question
Have you considered using arcpy.Copy_management and arcpy.Delete_managementto do this? – blah238 Oct 8 '12 at 16:32
Also if you have the geodatabase open in ArcMap, ArcCatalog, etc., you will not be able to delete it except within the application that has the lock on it. – blah238 Oct 8 '12 at 16:34
For the record, this is probably a good thing. .lock files generally indicate that something is in use. You don't want to delete something in use by an application. You want to stop using it first. (I consider it a weakness of the GDB format that you could go delete other files that belong to it while this one is locked.) Also, don't use distutils for file management; that's for creating distribution packages for Python code. – jpmc26 Dec 3 '15 at 22:32

Try using arcpy to back up your geodatabases. Example:

workspace = r"workspace\file\path"
arcpy.env.workspace = workspace
print "Set Workspace"
backup_location = r"new\folder\path" + "\\" + "workspace.gdb"
print "Backing up workspace"
arcpy.Copy_management(workspace, backup_location)
print "Clearing Workspace cache"
arcpy.env.workspace = ""
print "Deleting original copy"
print "Finished backing up"

Any time you use python to access/manipulate a geodatabase you create a lock on that geodatabase. By using arcpy you can copy, clear the lock, and delete to ensure it's all done properly.

share|improve this answer

You can try omitting the ".lock" extension in order to avoid removing those files. However, I think you may have to use the cleanup function through ArcObjects to remove everything.

folder = "folder path"
for the_file in os.listdir(folder):
    if ".lock" not in the_file:
        file_path = os.path.join(folder, the_file)
            if os.path.isfile(file_path):
        except Exception, e:
share|improve this answer
The fact you can't delete the .lock file means something is using the file. You shouldn't go trying to delete all the files in the GDB while something is using it. – jpmc26 Dec 3 '15 at 22:36

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.