More

Why Python says shapefile does not exist?


I'm working on a script that prints the names of hospitals that are within a certain zip-code. When I run my code, though, python keeps giving me an Error message that says that my Hospitals shapefile does not exist. I've tested to see whether or not the shapefile exists using arcpy.Exists and I get the answer "True". I was also wondering if it might be because the file is locked in some way but I've tried deleting my objects and cursors as well as closing arcgis and restarting python and nothing has changed.

Can anyone tell me why Python is not recognizing my shapefile?

Code for reference:

import arcpy arcpy.env.workspace=r"C:UsersCaraDocumentsMPSGISProgramming and ScriptingLab6Lab 6 Data" work=arcpy.env.workspace zip5=78705 strZip=str(zip5) arcpy.env.overwriteOutput = True def searchHospitals(work,zip5): fc= "Hospitals.shp" #create a list to store values hospitals=[] whereClause= '"ZIPCODE" LIKE'+"'%s%%'"% strZip sCursor= arcpy.SearchCursor(fc, whereClause, "", "", "") for row in sCursor: rowHospital = row.getValue("NAME") #if rowHospital value is not already in hospitals, append. if (not(rowHospital in hospitals)): hospitals.append(rowHospital) #number of hospitals in list count= len(hospitals) #print results print hospitals print "Total %s hospitals were found in zip-code %s"%(count, zip5) del row, sCursor

Here is the error message:

searchHospitals(work,zip5) Traceback (most recent call last): File "", line 1, in  searchHospitals(work,zip5) File "C:UsersCaraDocumentsMPSGISProgramming and ScriptingPython Scriptscursor_objects.py.py", line 98, in searchHospitals sCursor= arcpy.SearchCursor(fc, whereClause, "", "", "") File "C:Program Files (x86)ArcGISDesktop10.2arcpyarcpy\__init__.py", line 1167, in SearchCursor return gp.searchCursor(dataset, where_clause, spatial_reference, fields, sort_fields) File "C:Program Files (x86)ArcGISDesktop10.2arcpyarcpygeoprocessing\_base.py", line 359, in searchCursor self._gp.SearchCursor(*gp_fixargs(args, True))) IOError: "Hospitals.shp" does not exist

As long as your query is structured correctly this should work. I used the da.SearchCursor instead because I noticed you were on 10.2. I also removed work from the function parameters because it wasn't being used anywhere.

import arcpy arcpy.env.overwriteOutput = True workspace = "C:UsersCaraDocumentsMPSGISProgramming and ScriptingLab6Lab 6 Data" arcpy.env.workspace = workspace fc = "Hospitals.shp" def searchHospitals(zipcode): #create a list to store values hospitals = [] whereClause= '"ZIPCODE" LIKE' + "'%s%%'" % str(zipcode) with arcpy.da.SearchCursor(fc, ("NAME"), whereClause) as sCursor: for row in sCursor: rowHospital = str(row[0]) #if rowHospital value is not already in hospitals, append. if rowHospital not in hospitals: hospitals.append(rowHospital) #number of hospitals in list count = len(hospitals) #print results print hospitals print "Total {0} hospitals were found in zip code {1}".format(count, zipcode) #runs the function for zip code 55555 searchHospitals(55555)

I would probably go with ian's answer using the da cursor, but if you just want to 'fix' your script using the legacy cursor, it appears the dataset parameter isn't using your set workspace --- your workaround may be to enter the full path to your dataset. If you import os at the beginning of the script you can try this line substitution in your function:

sCursor= arcpy.SearchCursor(os.path.join(work,fc), whereClause)


Watch the video: How to Make A Simple Game in Python For Beginners (October 2021).