4

I have created a script that splits a shapefile into multiple new shapefiles and adds them to an ArcMap document. When running the program a second time to overwrite those shapefiles, lock-files are created and the program cannot overwrite the files.

What I do is I open the document, clear out all layers from that document, then I try to delete all the old shapefiles and then overwrite them, lastly add the new shapefiles to the document, save and close.

The strange thing is that this happens on some computers and not other. I wonder what arcpy methods could cause such locking and how I can clear the lock?

Update: I have now tried to set env.overwriteOutput = True but without result. The code below will simply print the error message. When I close the IDLE python console the lock files disappear.

def delete_month_shape_file(file_path, *args):

    if arcpy.Exists(file_path):
        try:
            arcpy.Delete_management(file_path)
        except arcpy.ExecuteError:
            message.error("Could not delete month shapefile.")
    else:
        pass
1
  • @alex-tereshenkov I sometimes make some schema modification to the shape files (adding/removing fields). A more precise process is I import a csv -> table, and use a cursor to merge the data in master shape with the table into a new table. Then i use split analysis to split the data in that table into shape files based on a start and end date defined for each row in the table.
    – tseberik
    Nov 14 '14 at 12:24
2

Lockfiles for shapefiles are generally created to prevent other processes from writing to the same files. From the same process it is possible to overwrite (assuming arcpy.gp.overwriteOutput=true) the files. When the workspace is disconnected the lockfiles should disappear. This happens when the process (Arcmap or Python) dies, as the workspace is created from the workspacefactory singelton.

If no process that has the workspace open is still alive and the lockfiles are still there you can just delete them using any filesystem operation.

3
  • Thanks, I will try that. For ArcGIS 10.1 it seems they have replaced that property with arcpy.env.overwriteOutput.
    – tseberik
    Nov 14 '14 at 13:56
  • If there is a schema lock established on a shapefile, you cannot overwrite it though. Nov 14 '14 at 15:05
  • 4
    How do you get rid of the locks in your Python code?
    – MjonesGEO
    Jan 26 '15 at 23:09

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