First, I'm trying to call a function from a module (final_functions) I created in a separate script. However I receive the following error:
Traceback (most recent call last): File "C:\Python27\ArcGIS10.3\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 325, in RunScript
exec codeObject in __main__.__dict__ File "C:\EsriPress\Python\Data\Final\Test.py", line 8, in <module>
check = final_functions.FieldExists(fcname, fieldname) File "C:\EsriPress\Python\Data\Final\final_functions.py", line 6, in FieldExists
namelist = [] NameError: global name 'fc' is not defined
Here's the main script:
import arcpy
from arcpy import env
arcpy.env.workspace = "C:/EsriPress/Python/Data/Final/Scratch.gdb"
import final_functions
fclist = arcpy.ListFeatureClasses('Fire*')
for fc in fclist:
fc = fcname
fieldname = "SFCount"
check = final_functions.FieldExists(fcname, fieldname)
print check
Here is the module (final_functions.py) I'm importing:
def FieldExists(fcname, fieldname):
namelist = []
fields = arcpy.ListFields(fcname)
for field in fields:
namelist.append(field.name)
if fieldname in namelist:
return "Field: SFCount already present in Feature Class: {0}".format(fcname)
else:
arcpy.AddField_management(fcname, fieldname, "TEXT")
if __name__ == "__main__":
fclist = arcpy.ListFeatureClasses('Fire*')
for fc in fclist:
fieldname = "SFCount"
fcname = fc
checkfields = FieldExists(fc, fieldname)
print checkfields
When I run final_functions on its own, it works great. I'm only given the error when trying to call it in the main script.
And if you're wondering, I'm just checking a list of feature classes for the existence of a field, and adding a new field if it doesn't exist.