0

I'm doing an interface with ArcGIS 10.2 ando Python 2.7 scripts. I can create new tables with arcpy, but when I run a second time, to replace created tables I need to delete the existing tables to create news with the same name. So I was using a code that gives me error, because I think that delete function doesn't delete the table, as I thought. I thought that problem could be because os memory cache, so I used a function to clear it, but it doesn't work..

My Python code:

tab_cg_r="F:\ArcGIS\Trab\Model\Result_otim\C_R_otim.dbf"
arcpy.env.workspace = "F:\ArcGIS\Trab\Model\Result_otim"
arcpy.ClearWorkspaceCache_management()

if arcpy.Exists("C_R_otim_view"):
    arcpy.Delete_management("F:\ArcGIS\Trab\Model\Result_otim\C_R_otim.dbf")
    #arcpy.DeleteFeatures_management("F:\ArcGIS\Trab\Model\Result_otim\C_R_otim.dbf\C_R_otim_view")

arcpy.env.workspace = "F:\ArcGIS\Trab\Model\Result_otim"
arcpy.ClearWorkspaceCache_management()

arcpy.CreateTable_management('F:\ArcGIS\Trab\Model\Result_otim', "C_R_otim.dbf")

# Process: Add Field
arcpy.AddField_management(tab_cg_r, "Barr", "Text", "", "", "50", "", "NON_NULLABLE", "NON_REQUIRED", "")
arcpy.AddField_management(tab_cg_r, "Pt_M", "Float", "", "", 10, "", "NULLABLE", "NON_REQUIRED", "")
arcpy.AddField_management(tab_cg_r, "Longitude", "Float", "", "", 10, "", "NON_NULLABLE", "NON_REQUIRED", "")
arcpy.AddField_management(tab_cg_r, "Latitude", "Float", "", "", 10, "", "NON_NULLABLE", "NON_REQUIRED", "")
arcpy.DeleteField_management(tab_cg_r, 'Field1')
##
rows_t_cR = arcpy.InsertCursor("F:\ArcGIS\Trab\Model\Result_otim\C_R_ot.dbf")
for x in range(0, (len(N_name_LR))):
    row = rows_t_cR.newRow()
    rows_t_cR.insertRow(row)

arcpy.MakeTableView_management("F:\ArcGIS\Trab\Model\Result_otim\C_R_otim.dbf", "C_R_otim_view")

Error:

ERROR 000601: Cannot delete F:\\ArcGIS\Trab\Model\Result_otim\C_R_otim.dbf.  May be locked by another application. Failed to execute (Delete).

How can I do this?

  • I don't understand why you have presented your code in two parts rather than as a single code snippet. The first error is the invalid pathname in the first line presented. I cannot see how that could create a table. – PolyGeo Sep 6 '16 at 11:56
  • Without a coherent code snippet and the full error message that results from running it, there is no opportunity to try to help you with where you are stuck with your code that complies with our focussed Q&A format. Please revise the code that you have presented, and be sure to test it before re-presenting it. – PolyGeo Sep 6 '16 at 12:16
  • 1
    Couple of issues I can spot, your path string is incorrect, it should be F:\ you have F: Also you are creating a table (C_R_otim.dbf) and not a spatial dataset. But MakeFeatureLayer is designed to create layers not tables. So you need to be using MakeTableView. – Hornbydd Sep 6 '16 at 13:38
  • I update my question with changes that you told me @Hornbydd – S.Rai Sep 6 '16 at 16:09
1

By adding import arcpy to the beginning of your code, and replacing your *.dbf with a test DBF file, I tested your code and was not able to reproduce your error message:

import arcpy

tab_cg_r="C:\Temp\Test.dbf"
arcpy.env.workspace = "C:\Temp"
arcpy.ClearWorkspaceCache_management()

if arcpy.Exists("C_R_otim_view"):
    arcpy.Delete_management("C:\Temp\Test.dbf")

arcpy.env.workspace = "C:\Temp"
arcpy.ClearWorkspaceCache_management()

arcpy.CreateTable_management('C:\Temp', "Test.dbf")

# Process: Add Field
arcpy.AddField_management(tab_cg_r, "Barr", "Text", "", "", "50", "", "NON_NULLABLE", "NON_REQUIRED", "")
arcpy.AddField_management(tab_cg_r, "Pt_M", "Float", "", "", 10, "", "NULLABLE", "NON_REQUIRED", "")
arcpy.AddField_management(tab_cg_r, "Longitude", "Float", "", "", 10, "", "NON_NULLABLE", "NON_REQUIRED", "")
arcpy.AddField_management(tab_cg_r, "Latitude", "Float", "", "", 10, "", "NON_NULLABLE", "NON_REQUIRED", "")
arcpy.DeleteField_management(tab_cg_r, 'Field1')
##
rows_t_cR = arcpy.InsertCursor("C:\Temp\Test.dbf")
for x in range(0, (len(N_name_LR))):
    row = rows_t_cR.newRow()
    rows_t_cR.insertRow(row)

arcpy.MakeTableView_management("C:\Temp\Test.dbf", "C_R_otim_view")

Instead I got:

Traceback (most recent call last):
  File "C:\Temp\test.py", line 23, in <module>
    for x in range(0, (len(N_name_LR))):
NameError: name 'N_name_LR' is not defined

I could keep fixing things as I find them by guessing what you may be doing but I think the onus should be on you to provide us with code snippets that work up to where you are stuck.

In any event, I think the problem may be that you are running your code when you have another application - perhaps ArcMap, perhaps a Python IDE, or perhaps something else holding a lock on your F:\\ArcGIS\Trab\Model\Result_otim\C_R_otim.dbf file. This is indicated in the error message that you received which included:

May be locked by another application

I recommend closing all applications, even rebooting if this particular error persists, and then running just your test. That way there should be only the one application running.

I do not think that you are seeing ERROR 000601 for the same reason as Using os.remove in ArcGIS Python script tool? because that question is using ArcSDE when there is no suggestion in your question that you may be too. ClearWorkspaceCache is only used with enterprise geodatabases (ArcSDE) and so is not necessary in your code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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