Adding shapefiles to a project in PRO using python

5211
11
Jump to solution
10-08-2020 12:52 PM
JamesWhite5
New Contributor III

Good afternoon.

I have a python script that I am using to obtain kml data and then turn that into a geodatabase, extract the information and create 3 shapefiles which I then need to publish as a web layer.  I have the script so that it does not automatically add the created geodatabase or shapefile automatically as some cannot be published as feature layers.  I am trying to get just the shapefiles to the map.  The snippet I am using is this:

aprx = arcpy.mp.ArcGISProject("Current")
map = aprx.listMaps()[0]
map.addDataFromPath('C:/directory/filename.shp')

I found a post from bixb0012 which I also tried, from 2017, which is this:

shp_path = # path to shape file

aprx = arcpy.mp.ArcGISProject("CURRENT")
map = aprx.listMaps()[0]  # assumes data to be added to first map listed
map.addDataFromPath(shp_path)

 And it did not work either.

The error I receive on both is this:

Traceback (most recent call last) In  [3]: Line 44:    map.addDataFromPath('C:/GIS_Projects/VMRC_KMZ/VMRC_Oyster_Clam_Data/shp/VMRC_Public_Clamming_Grounds.shp')  File c:\program files\arcgis\pro\Resources\arcpy\arcpy\_mp.py, in addDataFromPath: Line 1779:  return convertArcObjectToPythonObject(self._arc_object.addDataFromPath(*gp_fixargs((data_path,), True))) RuntimeError: ---------------------------------------------------------------------------

 

I am using PRO version 2.6.1.

Thank you for any help / suggestions.

Tags (1)
0 Kudos
11 Replies
JoshuaBixby
MVP Esteemed Contributor

With OS paths, it is always best to use raw strings.  That said, I would consider what you encountered a defect in the tool you are using.  Somewhere the tool is handling the non-raw string differently/incorrectly.

0 Kudos
JamesWhite5
New Contributor III

Ok, cool.  Thank you so much for your help.  I will start using raw strings instead.

0 Kudos