I am trying to create a script to add values to a new field in a table based on the text values of another field in the same table. Here is my code:
import os
import arcpy
from arcpy import env
arcpy.env.workspace = "c:/Myfiles/geodatabase"
updateFields = ["fieldname1","fieldname2"]
def updateweight(filename,fields):
with arcpy.da.UpdateCursor(filename,fields) as cursor:
for row in rows:
if row[0] == "unrestricted":
row[1] = 3
elif row[0] == "restricted":
row[1] = 2
elif row[0] == "severely restricted":
row[1] = 0
Cursor.updateRow(row)
del cursor
updateweight("shape_file_name", updateFields)
When I run this I get:
Run time error
File "<string>" line 1 in <module>
File "<string?" line 11 in updateweight cannot open 'shape_file_name'
I've tried including the .dbf file extension in the file name and also tried calling the assigning a variable to the filename and calling it that way.
I was unable to copy and paste the code directly because it is on a work machine in a secure environment, so there my be some typos in the code. I wrote it as a function so I can make it into a more generic tool once I've tested it.
-
2I assume you mean feature class not shapefile. You can't store shapefiles in a file geodatabase (FGDB). You can have feature classes in an FGDB (or in a feature dataset in an FGDB). If you have copied an actual shapefile (i.e the "shape_file_name.shp", "shape_file_name.shx" and "shape_file_name.dbf" files) into the file geodatabase directory, then that def. won't work.user2856– user28562019年09月18日 23:26:15 +00:00Commented Sep 18, 2019 at 23:26
1 Answer 1
I think that error is telling you that it cannot find a shapefile named "shape_file_name" in the current workspace.
I suspect that you may have set a variable named shape_file_name
in code outside what you have presented and then tried to refer to it as "shape_file_name" instead of shape_file_name
.
-
I used "shape_file_name" here as a generic name, in the script I called the actual name, sorry if that caused confusion.Greg Noland– Greg Noland2019年09月18日 23:19:10 +00:00Commented Sep 18, 2019 at 23:19
-
@GregNoland perhaps review gis.meta.stackexchange.com/questions/4312/…2019年09月18日 23:20:30 +00:00Commented Sep 18, 2019 at 23:20
-
I can see the shapefile in a feature dataset in the geodatabbase in arc catalog.Greg Noland– Greg Noland2019年09月18日 23:20:58 +00:00Commented Sep 18, 2019 at 23:20
-
@GregNoland Shapefiles cannot exist within geodatabases but feature classes can, and feature classes can also exist within feature.datasets of geodatabases.2019年09月18日 23:22:13 +00:00Commented Sep 18, 2019 at 23:22
-
Thanks for link, I'll keep it handy for my next question. I guess I wasn't fully aware of the difference between the two, which is probably the problem. Now that I think of it, it is likely a feature class. Should exporting it as a shapefile, then calling the correct path fix the issue?Greg Noland– Greg Noland2019年09月18日 23:29:52 +00:00Commented Sep 18, 2019 at 23:29