0

I'm getting <type 'exceptions.NameError'>: name 'B' is not defined when I run this script.

Why is my 'B' not defined and is it a syntax error?

For clarification, I am trying to get the code to loop through specific shapefiles in the folder "\\My Documents\ArcGIS\..." and copy them into "\\My Documents\NewFolder\..." The A variable represents a list of the specific shapefiles I want to copy from a folder and into a new one; the issue I am having is defining B, where the individual variable name from A is automatically carried over/read in any part of the code. I am trying to tell Arc that every time it sees "+STATE+" (which should be seen as B: main issue) it must insert the specific variable name listed in A, process it (in this case copy), and then loop back to the next specified variable, until it reaches the end of the list.

I would like to copy the 'FL_RechargeArea' through 'GA_AreaofContribution' shapefiles from the "\\My Documents\ArcGIS\..." folder into the "\\My Documents\NewFolder\..." and get it to save as '(specific-varialbe-name)2.shp'.

It's much less about copying and more about being able to loop through and process specific shapefile names in a folder, rather than looping through and processing perhaps the entire list within a folder.

# Import arcpy module
import arcpy

for A in B:

    # Local variables:
    A = ['FL_RechargeArea','FL_AquiferProtectionArea','FL_AreaofContribution','GA_RechargeArea','GA_AquiferProtectionArea','GA_AreaofContribution']
    B = "STATE"
    New_Shapefile = "\\\\My Documents\\ArcGIS\\"+STATE+".shp"
    Output_from_New_Shapefile = "\\\\My Documents\\NewFolder\\"+STATE+"2.shp"

    # Process: Copy Features
    arcpy.CopyFeatures_management(New_Shapefile, Output_from_New_Shapefile, "", "0", "0", "0")
2
  • 1
    What are you trying to accomplish with this script?
    – Aaron
    Commented Jul 9, 2014 at 21:55
  • you are getting this error because b is not defined on the 'for a in b:` line. Also, you code doesn't really make sense. You need to restructure the code, and you can only do that, once you are clear what you want to do. Commented Jul 10, 2014 at 8:47

1 Answer 1

2

You are referring to the variables by starting iterating items in your code before you actually declare the variables.

The correct syntax would be:

#Import arcpy module
import arcpy

# Local variables:
A =['FL_RechargeArea','FL_AquiferProtectionArea','FL_AreaofContribution','GA_RechargeArea','GA_AquiferProtectionArea','GA_AreaofContribution']
B = "STATE"
New_Shapefile = "\\\\My Documents\\ArcGIS\\"+STATE+".shp"
Output_from_New_Shapefile = "\\\\My Documents\\NewFolder\\"+STATE+"2.shp"

for A in B:
    # Process: Copy Features
    arcpy.CopyFeatures_management(New_Shapefile, Output_from_New_Shapefile, "", "0", "0", "0")

Another thing is that you want to give your variable names that make sense. Please refer to the Python PEP-8 style guide.

Your script however will not copy any shapefiles by using the a and b variables because you don't use them in the code. You probably want to use str.format() method to insert the variable values in the output shapefile name. Look for some example on using the str.format() code here and in the Esri Help.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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