getting runtime error using arcpy

2309
18
09-18-2017 01:58 PM
DanMacDonald
New Contributor

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.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((f.name, []) for f in arcpy.ListFields(LayerNeedsFieldsTurnedOff) if not f.required)

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

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

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

# 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')
else:
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
refLyr.name = LayerNeedsFieldsTurnedOff.name
lyrfile = "C:/temp/temp.lyr"
print LayerNeedsFieldsTurnedOff.name
arcpy.ApplySymbologyFromLayer_management(refLyr, lyrfile)
arcpy.mapping.UpdateLayer(df, LayerNeedsFieldsTurnedOff, refLyr, False)
# mxd.save()
print 'cleaning up-'
if arcpy.Exists('temp_layer'):
print '\'temp_layer\' still in memory...deleting now...'
arcpy.Delete_management('temp_layer')
print 'deleting obj refs...'
del LayerNeedsFieldsTurnedOff, refLyr, fields
print 'done.'

0 Kudos
18 Replies
RandyBurton
MVP Regular Contributor

Since the code is not properly indented, it is difficult to offer a suggestion.  Formatting your code when posting to Geonet will help: Code Formatting... the basics++

Also, can you include the error message text?

DanPatterson_Retired
MVP Esteemed Contributor

Dan could you format the code so that it is readable and syntax can be checked...

https://community.esri.com/blogs/dan_patterson/2016/08/14/script-formatting

DanMacDonald
New Contributor

script code

0 Kudos
DarrenWiens2
MVP Honored Contributor

What exactly is the error message?

0 Kudos
DanMacDonald
New Contributor

Runtime error

Traceback (most recent call last):

File "<string>", line 19, in <module>

File "c:\program files (x86)\arcgis\desktop10.3\arcpy\arcpy\arcobjects\arcobjects.py", line 1100, in getValue

return convertArcObjectToPythonObject(self._arc_object.GetValue(*gp_fixargs(args)))

RuntimeError: ERROR 999999: Error executing function.

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

guessing on the line numbers, but the line

fields = dict....

can you print the results after that line, because if it returns None, then the search cursor will fail

0 Kudos
DanMacDonald
New Contributor

It is down lower in the script  the following

fields.append(row.getValue(f))

0 Kudos
RandyBurton
MVP Regular Contributor

Looks like the line Dan Patterson‌ is referring to is after the first "elif". Formatting the code for Geonet will provide line numbers and help the discussion.

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

print statements would still help

0 Kudos