Not sure what the issues is but I am trying to use arcpy.da.InsertCursor on a SDE feature class but keep getting the 'Can't open workspace' error.The feature-dateset is versioned.
edit = arcpy.da.Editor(workspace)
RuntimeError: cannot open workspace
fc = r"C:\Users\***\AppData\Roaming\ESRI\Desktop10.8\ArcCatalog\***_***_***.sde\***.***.TEST\C***.***.Test"
workspace = os.path.dirname(fc)
#arcpy.env.workspace = r"C:\\Users\\***\\AppData\\Roaming\\Esri\\ArcGISPro\\Favorites\\Database Connections\\***_***.sde\\***.***.TEST"
#fc = "***.***.Test"
edit = arcpy.da.Editor(workspace)
# Edit session is started without an undo/redo stack for versioned data
# (for second argument, use False for unversioned data)
edit.startEditing(True)
# Start an edit operation
edit.startOperation()
dsc = arcpy.Describe(fc1)
fields = dsc.fields
# List all field names except the OID field
fieldnames = [field.name for field in fields]
# Create cursors and insert new rows
with arcpy.da.SearchCursor(fc1,fieldnames) as sCur:
with arcpy.da.InsertCursor(fc,fieldnames) as iCur:
for row in sCur:
iCur.insertRow(row)
del sCur
# Stop the edit operation.
edit.stopOperation()
# Stop the edit session and save the changes
edit.stopEditing(True)
Solved! Go to Solution.
Your fc path includes the feature dataset but when you use os.path.dirname() it steps up to the feature dataset. You need to use the geodatabase as your workspace for Editor, not the feature dataset. Try something like this
workspace = r"C:\Users\***\AppData\Roaming\ESRI\Desktop10.8\ArcCatalog\***_***_***.sde"
fc = os.path.join(geodatabase, "***.***.TEST", "C***.***.Test")
edit = arcpy.da.Editor(workspace)
As a matter of style, I prefer not to use words like workspace as a variable since it's in the env method. I always use ws as the workspace. Also, I notice that you use the r'' (raw text) but include \\. That's the beauty of r''; there is no need to escape a slash. Finally, what is the purpose of nesting the insert cursor within a search cursor?
import arcpy
arcpy.env.workspace = r'C:\temp\my.gdb'
ws = arcpy.env.workspace
fc = 'YourFeatureClassName'
...
...
...
edit = arcpy.da.Editor(ws)
edit.startEditing()
edit.startOperation()
...
...
...
edit.stopOperation()
edit.stopEditing(True)
Thanks for the replay. I have being trying different workspace formats, with r and \\, / etc. Thanks for the recommendations. I made changes based on your recommendations and I still get the darn 'cannot open workspace' error.
I am trying to see if inserCursor is faster then Append. In my current code the Append process just take to long so I thought I would try insertCursor.
If you use append, set it and forget it; that is, create a script for the append, and start it at 4:55 pm just be fore you leave for the day and forget about it. That saves you all the overhead of insert cursors, edit sessions, etc....
Hi,
Usually the workspace is set to whatever the database is since that is where the edit session usually occurs, then you can specify the feature class(s) that you want to edit in that workspace.
import arcpy
arcpy.env.OverwriteWorkspace = True
workspace = r'database.sde' # or database.gdb
walk = arcpy.da.Walk(workspace, datatype="FeatureClass")
fcs = []
edit = arcpy.da.Editor(workspace)
edit.startEditing(False, True)
edit.startOperation()
for dirpath, dirnames, filenames in walk:
for filename in filenames:
fcs.append(os.path.join(dirpath, filename))
for fc in fcs:
geometryType = arcpy.Describe(fc).shapeType
fcsname = os.path.basename(fc)
name = os.path.splitext(fcsname)
y = name[1].lstrip('.')
print (y)
#Insert code here#
edit.stopOperation()
edit.stopEditing(True)
This is what I typically do when editing a database. Hope this helps.
Your fc path includes the feature dataset but when you use os.path.dirname() it steps up to the feature dataset. You need to use the geodatabase as your workspace for Editor, not the feature dataset. Try something like this
workspace = r"C:\Users\***\AppData\Roaming\ESRI\Desktop10.8\ArcCatalog\***_***_***.sde"
fc = os.path.join(geodatabase, "***.***.TEST", "C***.***.Test")
edit = arcpy.da.Editor(workspace)
I did try the following.
workspace = r"C:\Users\***\AppData\Roaming\ESRI\Desktop10.8\ArcCatalog\***_***_***.sde"
fc1 = os.path.join("***.***.TEST","***.***.Test")
&
workspace = r"C:\Users\***\AppData\Roaming\ESRI\Desktop10.8\ArcCatalog\***_***_***.sde"
fc1 = "***.***.TEST","***.***.Test"
But adding the workspace to the os.path.join is what allowed me to edit.
workspace = r"C:\Users\***\AppData\Roaming\ESRI\Desktop10.8\ArcCatalog\***_***_***.sde"
fc1 = os.path.join(workspace,"***.***.TEST","***.***.Test") #adding workspace worked.
I get "False" when I added the print(arcpy.Exists(workspace)).
Well that's a problem too. It should print True