AnsweredAssumed Answered

What to do when a python script fails when a folder/GDB/MDB contains lock file

Question asked by kjdowdy4026 on Apr 6, 2015
Latest reply on Apr 6, 2015 by sephefox

So I have a python script the crawls though a parent directory and compacts all personal and file geodatabases.  However, if there is a lock on any of the GDBs, the script fails.  What's the best way to have the script skip over any GDBs that contain lock files?  Using ESRI 10.2.2.  Here is the script:

 

import arcpy, os, sys


#set workspace
arcpy.env.workspace = arcpy.GetParameterAsText(0)


#total size of GDBs in workspace before compact
def get_size(start_path = arcpy.env.workspace):
    total_size = 0
    for dirpath, dirnames, filenames in os.walk(start_path):
        for f in filenames:
            fp = os.path.join(dirpath, f)
            total_size += os.path.getsize(fp)
    return round((total_size / float(1048576)), 2)


print "Total Size before Compact " + str(get_size()) + "mb"
results = open("R:\\GIS\\Compact_Results\\results.txt","w")
results.write("Total Size before Compact " + str(get_size()) + "mb" + '\n')


feature_classes = []


#loop through all folders in the workspace
for dirpath, dirnames, filenames in arcpy.da.Walk(arcpy.env.workspace, datatype="Container"):
    for dirname in dirnames:
        if ".gdb" in dirname:
            #compact all gdbs in the user defined worspace
            arcpy.Compact_management(os.path.join(dirpath, dirname))
            print "Successfully compacted " + dirpath + "\ " + dirname
            results.write("Successfully compacted " + dirpath + "\ " + dirname + '\n')
    for dirname in dirnames:
        if ".mdb" in dirname:
            #compact all mdbs in the user defined worspace
            arcpy.Compact_management(os.path.join(dirpath, dirname))
            print "Successfully compacted " + dirpath + "\ " + dirname
            results.write("Successfully compacted " + dirpath + "\ " + dirname + '\n')            




#total size of GDBs in workspace after compact
def get_size(start_path = arcpy.env.workspace):
    total_size = 0
    for dirpath, dirnames, filenames in os.walk(start_path):
        for f in filenames:
            fp = os.path.join(dirpath, f)
            total_size += os.path.getsize(fp)
    return round((total_size / float(1048576)), 2)


print "Total Size after Compact " + str(get_size()) + "mb"
results.write("Total Size after Compact " + str(get_size()) + "mb" + '\n')
results.close()

 

Thanks for taking a look and any help!

Outcomes