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.
2 Answers 2
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.
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)
-
2The 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– jpmc262015年12月03日 22:36:58 +00:00Commented Dec 3, 2015 at 22:36
Explore related questions
See similar questions with these tags.
arcpy.Copy_management
andarcpy.Delete_management
to do this?.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 usedistutils
for file management; that's for creating distribution packages for Python code.