3

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:\Users\Cara\Documents\MPSGIS\Programming and Scripting\Lab6\Lab 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 "<pyshell#4>", line 1, in <module>
    searchHospitals(work,zip5)
  File "C:\Users\Cara\Documents\MPSGIS\Programming and Scripting\Python Scripts\cursor_objects.py.py", line 98, in searchHospitals
    sCursor= arcpy.SearchCursor(fc, whereClause, "", "", "")
  File "C:\Program Files (x86)\ArcGIS\Desktop10.2\arcpy\arcpy\__init__.py", line 1167, in SearchCursor
    return gp.searchCursor(dataset, where_clause, spatial_reference, fields, sort_fields)
  File "C:\Program Files (x86)\ArcGIS\Desktop10.2\arcpy\arcpy\geoprocessing\_base.py", line 359, in searchCursor
    self._gp.SearchCursor(*gp_fixargs(args, True)))
IOError: "Hospitals.shp" does not exist
  • 1
    Can you please provide the error message? – Emil Brundage Jul 26 '15 at 5:53
  • So that we do not have to guess what you might be entering as values for work and zip5 would you be able to hardcode them into your code snippet, please? You don't say how you are running this code, but for debugging purposes what happens if you run it from IDLE? Doing that should give you the line number along with the precise error. – PolyGeo Jul 26 '15 at 6:16
2

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:\\Users\\Cara\\Documents\\MPSGIS\\Programming and Scripting\\Lab6\\Lab 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)
1

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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.