0

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.

3
  • 2
    Have you considered using arcpy.Copy_management and arcpy.Delete_managementto do this?
    – blah238
    Commented Oct 8, 2012 at 16:32
  • 3
    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
    Commented Oct 8, 2012 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
    Commented Dec 3, 2015 at 22:32

2 Answers 2

1

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 = ""
arcpy.ClearWorkspaceCache_management(workspace)
print "Deleting original copy"
arcpy.Delete_management(workspace)
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.

0

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)
        try:
            if os.path.isfile(file_path):
                os.unlink(file_path)
            else:
                shutil.rmtree(file_path)
        except Exception, e:
            arcpy.AddMessage(e)
1
  • 2
    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
    Commented Dec 3, 2015 at 22:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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