Using pathlib with SearchCursor

339
3
Jump to solution
06-24-2021 09:49 AM
BlakeTerhune
MVP Regular Contributor

I'm trying to switch from using the os module to pathlib.Path for working with file paths. For some reason, when I construct the path to the feature class (or table) with Path, it fails with

RuntimeError: 'in_table' is not a table or a featureclass

If I simply convert the path to string in the cursor using str(), it works fine. A print() of the path looks normal; not a weird object or something. I get the same behavior with a file geodatabase and Oracle 12c enterprise geodatabase. I also tried using Path.joinpath() instead of the shorthand / but had the same error.

Is there a different way I should be constructing paths?

 

from pathlib import Path

egdb_conn = r"C:\GISConnections\user@instance.sde"
fc_path = Path(egdb_conn) / "schema.tablename"

print(fc_path)
print(arcpy.Exists(fc_path))

with arcpy.da.SearchCursor(fc_path, ["OID@"]) as search_cursor:
    for row in search_cursor:
        print(row)

 

BTW, I'm on ArcGIS Pro 2.8

0 Kudos
1 Solution

Accepted Solutions
DanPatterson
MVP Esteemed Contributor

It returns a different object type, print works since its __repr__ method returns a string

.... pathlib.WindowsPath

convert to string before arc'ing it


... sort of retired...

View solution in original post

3 Replies
JoshuaBixby
MVP Esteemed Contributor

Not surprising, the ArcPy DA module was originally written before pathlib came around.  I am guessing Esri will view this as an Enhancement Request to support pathlib.

DanPatterson
MVP Esteemed Contributor

It returns a different object type, print works since its __repr__ method returns a string

.... pathlib.WindowsPath

convert to string before arc'ing it


... sort of retired...

View solution in original post

BlakeTerhune
MVP Regular Contributor

Thank you both for sharing your thoughts. I have created a new Python Idea.

Update arcpy cursors to work with paths constructe... - Esri Community

0 Kudos