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?
1 Answer 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!
arcpy.env.workspace = "project.gdb"
witharcpy.env.workspace = os.path.join(WorkingDirectory,"project.gdb")
. You will need toimport os
import os
you could usearcpy.env.workspace = r"{0}\{1}".format(WorkingDirectory,"project.gdb")
instead.