I am attempting to run a nested for loop to clip habitat types by Marine Protected Area (MPA) region offshore California. When running the code below I receive error 000732 (red text). Any help is greatly appreciated.
File "C:\Python27\ArcGIS10.3\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 326, in RunScript
exec codeObject in __main__.__dict__
File "E:\BlueCarbon\GIS\Script\MPARegionClip.py", line 19, in <module>
arcpy.Clip_analysis(fc, reg, op_FC)
File "C:\Program Files (x86)\ArcGIS\Desktop10.3\ArcPy\arcpy\analysis.py", line 56, in Clip
raise e
ExecuteError: Failed to execute. Parameters are not valid.
ERROR 000732: Clip Features: Dataset CentralCoastProj.shp does not exist or is not supported
Failed to execute (Clip).
CODE:
import arcpy
from arcpy import env
#Set Variables
hab_Workspace = "E:\\BlueCarbon\\GIS\\Workspace\\habproj "
regWorkspace = "E:\\BlueCarbon\\GIS\\Workspace\\IndRegProj\\"
tWorkspace = "E:\\BlueCarbon\\GIS\\Workspace\\habproj_regions"
env.workspace = regWorkspace
region = arcpy.ListFeatureClasses("")
env.workspace = hab_Workspace
FClist = arcpy.ListFeatureClasses("")
print FClist
for fc in FClist:
for reg in region:
op_FC = tWorkspace + "\\" + fc + reg
arcpy.Clip_analysis(fc, reg, op_FC)
Solved! Go to Solution.
You're running into issues because you can only have one workspace environment at a time. You set the workspace to the region folder. The region list is populated with the names (not full paths) of the feature classes in the region workspace folder. Then, you switch the workspace to the hab workspace folder. The FClist is populated with feature class names (not full paths) of the feature classes in the hab workspace folder. Your loops cycle through the feature class names (not full paths) in your lists. Finally, your clip tool is run against two feature class names (not full paths). Since the tool is provided with feature class names (not full paths) it looks for feature classes in the workspace environment folder - your hab folder.
The most direct route I see to a solution is to store the full path in your lists, some thing like:
>>> import os ... arcpy.env.workspace = r'C:\junk' ... region = [os.path.join(arcpy.env.workspace,fc) for fc in arcpy.ListFeatureClasses("")]
I would put some print statements in to validate that the paths to your input and output data are correct. That usually slips me up at first when creating new python scripts.
I would agree with Michael, by the looks of it the shapefile at least can't be found. You may want to verify all the files used in the clip using Exists—Help | ArcGIS for Desktop prior to
You're running into issues because you can only have one workspace environment at a time. You set the workspace to the region folder. The region list is populated with the names (not full paths) of the feature classes in the region workspace folder. Then, you switch the workspace to the hab workspace folder. The FClist is populated with feature class names (not full paths) of the feature classes in the hab workspace folder. Your loops cycle through the feature class names (not full paths) in your lists. Finally, your clip tool is run against two feature class names (not full paths). Since the tool is provided with feature class names (not full paths) it looks for feature classes in the workspace environment folder - your hab folder.
The most direct route I see to a solution is to store the full path in your lists, some thing like:
>>> import os ... arcpy.env.workspace = r'C:\junk' ... region = [os.path.join(arcpy.env.workspace,fc) for fc in arcpy.ListFeatureClasses("")]
Wow that makes complete sense and thanks for your help!