1

I'm struggling with a very basic problem. it seems though, it's a problem with arcpy itself.

I'm running the following code and it's give me a problem in the line of:

with arcpy.da.UpdateCursor(main_points, "*" ) as cursor:

The log of the problem says is:

builtins.RuntimeError: cannot open 'main_points'

However if I remove the line of adding field:

arcpy.AddField_management(main_points, "tag_id", "SHORT", 10)

The script runs smoothly.

(PS: If I replace the line of adding field to delete field using "arcpy.DeleteField_management", the same problem happens giving me the same log error)

Here is my whole code:

import arcpy
WorkingDirectory = r"C:\Users\me\Documents"

arcpy.env.workspace = WorkingDirectory
arcpy.env.overwriteOutput=True
arcpy.CreateFileGDB_management(WorkingDirectory, "project.gdb")
arcpy.FeatureClassToGeodatabase_conversion(["main_points.shp"], "project.gdb")
arcpy.env.workspace = "project.gdb"
main_points = arcpy.ListFeatureClasses()[0]
arcpy.AddField_management(main_points, "tag_id", "SHORT", 10)
with arcpy.da.UpdateCursor(main_points, "*" ) as cursor:
    for row in cursor:
        print(row[0]) # just simple printing of Object ID
        cursor.updateRow(row)

How could I overcome this problem?

  • 2
    Try replacing arcpy.env.workspace = "project.gdb" with arcpy.env.workspace = os.path.join(WorkingDirectory,"project.gdb"). You will need to import os – BERA Jul 17 '18 at 9:59
  • 1
    Agree with @BERA you need to correct your workspace. A workspace should be a full path to a location, all you have done is set it to some string "project.gdb". Ask yourself where is "project.gdb"? As you have not provided the full path how is arcpy supposed to know where it is? – Hornbydd Jul 17 '18 at 10:10
  • If you don't want to import os you could use arcpy.env.workspace = r"{0}\{1}".format(WorkingDirectory,"project.gdb") instead. – PolyGeo Jul 17 '18 at 10:13
  • I used both of them and didn't work. I'll post the solution now! – Matt_Geo Jul 17 '18 at 10:54
1

It took me longer to find out, but it seems a bug for ArcGIS pro (Because the same code runs in ArcMap smoothly)

I used the following code and it worked somehow. It doesn't make sense at all in my opinion, but if I remove it the code doesn't work.

arcpy.da.Editor(arcpy.env.workspace)

PS: It need to be added for each time I use a "AddField_management" or "DeleteField_management" tool!

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.