In ArcGIS Pro 2.4.0, I noticed some unexpected behavior with forward slashes in data paths with code run in the Python console embedded in Pro.
In short, if backslashes are used, the code works:
d = "C:\gispy\data\ch02\park.shp" proj = arcpy.mp.ArcGISProject("CURRENT") m = proj.listMaps() m.addDataFromPath(d) <arcpy._mp.Layer object at 0x00000287876AE7F0> # Hurray! Data added to map.
But if forward slashes are used in the data path, it throws a RuntimError and fails to add data to the map:
d2 = "C:/gispy/data/ch02/park.shp" m.addDataFromPath(d2) Traceback (most recent call last): File "<string>", line 1, in <module> File "c:\program files\arcgis\pro\Resources\arcpy\arcpy\_mp.py", line 1531, in addDataFromPath return convertArcObjectToPythonObject(self._arc_object.addDataFromPath(*gp_fixargs((data_path,), True))) RuntimeError
I could not find a bug report on this. Are there plans to address this or is this intentional behavior? If so, why?
If it was found it is several versions old and a backport won't be provided. You could update to a newer version of arcgis pro to be safe.
If you want, you can go through the release notes for prior versions to see if it was reported as a bug and fixed.
The path with forward slashes is python valid, however, to play it safe with back slash paths, always use raw encoding
good = r"C:\temp\data\ch02\park.shp" # ---- a little `r` goes a long way bad = "C:\temp\data\ch02\park.shp" # ---- no `r` # ---- test prints print(good) C:\temp\data\ch02\park.shp print(bad) C: emp\data\ch02\park.shp # ---- a tab inserted # ---- abject total failure ugly = "C:\temp\data\ch02\x.shp" File "<ipython-input-11-6fa7135b60c0>", line 1 ugly = "C:\temp\data\ch02\x.shp" ^ SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 17-18: truncated \xXX escape
Hi Dan, Great to hear from you. Somehow, I always encounter you when researching arcpy issues online. I feel like I know you. 🙂 Yes, I'm familiar with the raw string approach and the perils of escape sequences. This is why I found it even more frustrating that Pro is not handling forward slashes correctly.
Good find! Probably the same underlying issue though.
This begs a few questions...This was submitted 14 years ago and the status is "Not in Current Product Plan"? What could this mean? Maybe a new bug report needs to be submitted. Can any old shmo create a bug report? Were they really working on ArcGIS Pro in 2006? Forward slashes have never been a problem before in Desktop, why start now?
I think any shmo can... however, I have never had any problem with forward slashes.
My default is "raw" encoding (using the little 'r' in scripts).
In geoprocessing tools, I don't type... I navigate and select files and let arc* wizardry do its work.
I would investigate further and see whether other paths fail with forward slashes. I am wondering if it just may be that particular folder, or something in the mp module (which I rarely use).
Give it shot and if it is reproduceable, then file a report ... however, the first thing that will be suggested is to upgrade to the latest version (2.6.3)