I'm trying to run a script tool in Arcmap that will reproject a number of shapefiles from the source data folder and place the newly projected shapefiles in a destination data folder.
I'm getting the following error 000732 with my arcpy script:
Here is my script (the error is coming from the final line):
import arcpy, os
from arcpy import env
source_data_folder = arcpy.GetParameterAsText(0)
out_coordinate_system = arcpy.GetParameterAsText(1)
destination_data_folder = arcpy.GetParameterAsText(2)
for (path, dirs, files) in os.walk(source_data_folder):
for file in files:
# find all .shp files in folder and all subfolders
if ".shp" in file.lower()[-4:]:
outpath = os.path.join(destination_data_folder, file)
arcpy.Project_management(file, outpath, out_coordinate_system)
The shapefile that the error is referring to ("Class_NS.shp") is the first shapefile in the "source_data_folder". So the script is running that far - it's finding the first shapefile, it's just claiming it "does not exist or is not supported".
The shapefile most definitely exists.
Does this mean it is not supported?
It's just a regular shapefile.
There may be errors with my code but so far the error message is not code-related.
-
Please always provide error messages as text rather than pictures so that they are available for future searches.PolyGeo– PolyGeo ♦2018年02月02日 00:51:48 +00:00Commented Feb 2, 2018 at 0:51
1 Answer 1
You just need to include the root path with the shapefile "file" name. os.walk
returns the root and then either the directory or file for every element in each folder - meaning that for each file variable in the loop, the name is only "somefile.shp" without any path information, which arcpy needs to process. So an additional line will get this to work as intended:
import arcpy, os
from arcpy import env
source_data_folder = arcpy.GetParameterAsText(0)
out_coordinate_system = arcpy.GetParameterAsText(1)
destination_data_folder = arcpy.GetParameterAsText(2)
for (path, dirs, files) in os.walk(source_data_folder):
for file in files:
# find all .shp files in folder and all subfolders
if ".shp" in file.lower()[-4:]:
input_shp = os.path.join(path, file)
outpath = os.path.join(destination_data_folder, file)
arcpy.Project_management(input_shp, outpath, out_coordinate_system)
-
3Consider using arcpy.da.Walk (source_data_folder, datatype=''FeatureClass") resources.arcgis.com/en/help/main/10.2/index.html#//…, that way you only get the Esri recognized feature classes and rasters, also fName,fExt = os.path.splitext(file) then if fExt.lower() == '.shp': to find only the shapefiles.Michael Stimson– Michael Stimson2017年11月14日 01:41:30 +00:00Commented Nov 14, 2017 at 1:41