Editing issues, edit = arcpy.da.Editor(workspace) RuntimeError: cannot open workspace

521
8
Jump to solution
07-14-2021 08:52 AM
2Quiker
Occasional Contributor II

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)

 

 

0 Kudos
1 Solution

Accepted Solutions
BlakeTerhune
MVP Regular Contributor

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)

View solution in original post

0 Kudos
8 Replies
JoeBorgione
MVP Esteemed Contributor

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)

 

can't wait to retire....
0 Kudos
2Quiker
Occasional Contributor II

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.

0 Kudos
JoeBorgione
MVP Esteemed Contributor

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....

can't wait to retire....
0 Kudos
RPGIS
by
Regular Contributor

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.

0 Kudos
BlakeTerhune
MVP Regular Contributor

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)

View solution in original post

0 Kudos
2Quiker
Occasional Contributor II

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.

 

0 Kudos
2Quiker
Occasional Contributor II

I get "False" when I added the print(arcpy.Exists(workspace)).

0 Kudos
BlakeTerhune
MVP Regular Contributor

Well that's a problem too. It should print True