Cursor Not Working

1148
12
11-18-2016 07:59 AM
alysaochoa2
New Contributor II

Hi everyone,

I'm writing a script to plot points and then delete rows that have a value of less than 5.

the script

I am getting the following error on the cursor lines:

Traceback (most recent call last):
File "C:\Users\30030839\Favorites\Downloads\pyfinal2.py", line 32, in <module>
cursor = arcpy.da.UpdateCursor("C:\\Users\\30030839\\Desktop\\Python\\capacity_points_layer2", ["DEM"])
RuntimeError: cannot open 'C:\Users\30030839\Desktop\Python\capacity_points_layer2'

I'm not sure why this file isn't able to open. It's not being used in any application, and its folder is not open.I added the file to ArcMap previously to make sure the shapefile worked and that the attribute table exists, and it's fine. Does anyone know why I would be getting this error?

0 Kudos
12 Replies
NeilAyres
MVP Frequent Contributor

So, what is this layer you are trying to run an update cursor on?

cursor = arcpy.da.UpdateCursor("C:\\Users\\30030839\\Desktop\\Python\\capacity_points_layer2", ["DEM"])

It is not a shapefile, nor anything in a file geodatabase, so what is it and does it exist.

0 Kudos
alysaochoa2
New Contributor II

It’s a .lyr file. To test, I added it to ArcMap to make sure it worked. It seems like everything is fine and it should work. I also tried the cursor with .lyr and it did not help.

[cid:image002.png@01D2417B.CC5CC3A0]

0 Kudos
NeilAyres
MVP Frequent Contributor

Well, then it should have ".lyr" on the end as the extension. Do yourself a favour and in the file explorer, view setting untick to options which says "hide extensions for known file types".

But, I think an update cursor should be run on the data itself. Not a layer file which is a superset (including defrinitions and symbology) and a pointer to the data itself.

So, where is the feature you are trying to update and what is it?

alysaochoa2
New Contributor II

The feature I am trying to update is saved in this path on my computer: C:
Users
30030839
Desktop
Python
capacity_points_layer2.lyr

It is a point shapefile. I have attached the point shapefile if that helps.

Regards,

Alysa Ochoa | Reporting Specialist, Business Intelligence | Bright Horizons Family Solutions | Phone: 720.644.2040 | alysa.ochoa@brighthorizons.com<mailto:alysa.ochoa@brighthorizons.com> | 2655 W. Midway Blvd., Suite 330 | Broomfield, CO 80020

0 Kudos
alysaochoa2
New Contributor II

Now I got it to recognize the file but I’m getting a different error:

All of the script seems logical and the file works fine. This is the first time I’m running into this problem, where the cursor is not working and I am not getting much information from the IDLE on why.

0 Kudos
MitchHolley1
MVP Regular Contributor

Can you post the updated code, please?

0 Kudos
alysaochoa2
New Contributor II

Here is the updated script. I have tested ALL other parts of the script and it works. Just the cursor is not working. 

According to this source from Esri, I should be able to just reference the layer to update the rows in the table. UpdateCursor—ArcPy Functions | ArcGIS for Desktop 

0 Kudos
DarrenWiens2
MVP Honored Contributor

Rather than pointing the cursor to the .lyr file, can you try pointing it to the feature class that the .lyr refers to?

0 Kudos
MitchHolley1
MVP Regular Contributor

Try this: 

with arcpy.da.UpdateCursor(newLayer, [field1]) as cursor:

Put the field variable in brackets.

0 Kudos