Select to view content in your preferred language

ArcPy "does not exist" issue

1639
3
09-15-2020 12:39 PM
JoelZemanek
New Contributor II

I'm learning ArcPy and do not understand why it says this file path does not exist...What am I doing wrong. In the end I'd like to accomplish code similar to this: 

import arcpy # Open a searchcursor # Input: C:/Data/Counties.shp # Fields: NAME; STATE_NAME; POP2000 # Sort fields: STATE_NAME A; POP2000 D rows = arcpy.SearchCursor("c:/data/counties.shp", fields="NAME; STATE_NAME; POP2000", sort_fields="STATE_NAME A; POP2000 D") # Iterate through the rows in the cursor and print out the # state name, county and population of each. for row in rows: print("State: {0}, County: {1}, Population: {2}".format( row.getValue("STATE_NAME"), row.getValue("NAME"), row.getValue("POP2000")))

- A MORE CLEAN VERSION CAN BE SEEN OVER THIS LINK: SearchCursor—Help | ArcGIS Desktop 

I attached a screen shot of my issues. 

0 Kudos
3 Replies
George_Thompson
Esri Frequent Contributor

You may want to move your post to a more appropriate space like python snippetsPythonArcGIS Pro‌ for a broader audience

--- George T.
JoshuaBixby
MVP Esteemed Contributor

You need to use raw-string literals or properly escape your path strings.  Instead of:

'C:\arcgis\ArcTutor\Geocoding\Atlanta.gdb'

use:

r'C:\arcgis\ArcTutor\Geocoding\Atlanta.gdb'
JoeBorgione
MVP Emeritus

A couple things to consider: when posting up Python code here on the Geonet, use the Syntax Highlighter so we can read the code.  See /blogs/dan_patterson/2016/08/14/script-formatting 

Also, don't use the old style Search Cursor; instead use the Data Access Search Cursor. Check out the entire Data Access Module.

This is what the code you provided above looks like when you properly format it with the Syntax Highlighter:

import arcpy

# Open a searchcursor
#  Input: C:/Data/Counties.shp
#  Fields: NAME; STATE_NAME; POP2000
#  Sort fields: STATE_NAME A; POP2000 D
rows = arcpy.SearchCursor("c:/data/counties.shp",
                          fields="NAME; STATE_NAME; POP2000",
                          sort_fields="STATE_NAME A; POP2000 D")

# Iterate through the rows in the cursor and print out the
# state name, county and population of each.
for row in rows:
    print("State: {0}, County: {1}, Population: {2}".format(
        row.getValue("STATE_NAME"),
        row.getValue("NAME"),
        row.getValue("POP2000")))

I think you'll agree it's a whole lot easier to read.

That should just about do it....