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
  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.

  • 2
    I 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 Sep 18 '19 at 23:26

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 Sep 18 '19 at 23:19
  • @GregNoland perhaps review gis.meta.stackexchange.com/questions/4312/… – PolyGeo Sep 18 '19 at 23:20
  • I can see the shapefile in a feature dataset in the geodatabbase in arc catalog. – Greg Noland Sep 18 '19 at 23:20
  • @GregNoland Shapefiles cannot exist within geodatabases but feature classes can, and feature classes can also exist within feature.datasets of geodatabases. – PolyGeo Sep 18 '19 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 Sep 18 '19 at 23:29

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.