AnsweredAssumed Answered

getting runtime error using arcpy

Question asked by dpmac66 on Sep 18, 2017
Latest reply on Sep 20, 2017 by dpmac66

Hi all
I am trying to write a arcpy script that will turn off all NULL and empty fields. The script runs for a while and then gives a run time error at fields[f].append(row.getValue(f)). Below is the script.


import arcpy
from arcpy import env
arcpy.env.overwriteOutput = True
arcpy.env.addOutputsToMap = False
arcpy.env.workspace = "C:/temp"
out_layer = "temp.lyr"

mxd = arcpy.mapping.MapDocument('CURRENT')
df = arcpy.mapping.ListDataFrames(mxd, '*')[0] # need to find way to get list of layers there can be more than one data frame
listfields = []
thebadlist = []
for LayerNeedsFieldsTurnedOff in arcpy.mapping.ListLayers(mxd):
if LayerNeedsFieldsTurnedOff.isGroupLayer:
print "group print"
elif LayerNeedsFieldsTurnedOff.isFeatureLayer:
fields = dict((, []) for f in arcpy.ListFields(LayerNeedsFieldsTurnedOff) if not f.required)

rows = arcpy.SearchCursor(LayerNeedsFieldsTurnedOff,"","","","")
for row in rows:
for f in fields.keys():
fields[f].append(row.getValue(f)) #**the error happens here**
#print row.getValue(f)

for field, values in fields.iteritems():
#print field

if field == "Shape":
elif all(map(lambda s: s is None or not str(s).strip(), values)):

# LayerNeedsFieldsTurnedOff = arcpy.mapping.ListLayers(mxd, 'exampledata', df)[0]
# fill in your desired fields to remain visible

desiredFields = listfields

field_info = arcpy.Describe(LayerNeedsFieldsTurnedOff).fieldInfo # I need this for field count

for i in range(field_info.count):
if field_info.getfieldname(i) not in desiredFields:
if field_info.getfieldname(i) == "SHAPE":
field_info.setvisible(i, 'VISIBLE')
elif field_info.getfieldname(i) == "Shape":
field_info.setvisible(i, 'VISIBLE')
field_info.setvisible(i, 'HIDDEN')

arcpy.SaveToLayerFile_management(LayerNeedsFieldsTurnedOff, out_layer)
arcpy.MakeFeatureLayer_management(LayerNeedsFieldsTurnedOff, 'temp_layer', '', '', field_info)
refLyr = arcpy.mapping.Layer('temp_layer')
# rename the ref layer the same as your target layer =
lyrfile = "C:/temp/temp.lyr"
arcpy.ApplySymbologyFromLayer_management(refLyr, lyrfile)
arcpy.mapping.UpdateLayer(df, LayerNeedsFieldsTurnedOff, refLyr, False)
print 'cleaning up-'
if arcpy.Exists('temp_layer'):
print '\'temp_layer\' still in memory...deleting now...'
print 'deleting obj refs...'
del LayerNeedsFieldsTurnedOff, refLyr, fields
print 'done.'