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

2341
10
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
10 Replies
JoeBorgione
MVP Emeritus

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)

 

That should just about do it....
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 Emeritus

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

That should just about do it....
0 Kudos
RPGIS
by
Occasional Contributor III

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)
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
ajreeve
New Contributor
fc = os.path.join(geodatabase, "***.***.TEST", "C***.***.Test")

What is "geodatabase"? Also why is there two feature classes after that?

Thank you in advance!

0 Kudos
BlakeTerhune
MVP Regular Contributor

@ajreeve wrote:
fc = os.path.join(geodatabase, "***.***.TEST", "C***.***.Test")

What is "geodatabase"? Also why is there two feature classes after that?

Thank you in advance!


Hi @ajreeve,

os.path.join takes a variable number of parameters. The first is the starting portion of the path and everything else will get combined with the starting path separated by a slash to build the whole path. In this case, geodatabase is a variable assumed to be defined somewhere else in the code (this is just a snippet). It could be something like:

geodatabase = r"C:\temp\mydata.gdb"

The next to variables are the feature dataset name ("***.***.TEST"), and then the feature class name ("C***.***.Test"). So the fc variable would end up with something like:

"C:\temp\mydata.gdb\C***.***.Test\***.***.TEST"

EDIT:

I think the code in my post is slightly misleading. It should probably be:

workspace = r"C:\Users\***\AppData\Roaming\ESRI\Desktop10.8\ArcCatalog\***_***_***.sde"

fc = os.path.join(workspace, "***.***.TEST", "C***.***.Test")

edit = arcpy.da.Editor(workspace)
0 Kudos
2Quiker
Occasional Contributor II

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

0 Kudos