2

I am having trouble getting the List Fields function to work. I need to search through a number of shapefiles located in different folders to list the fieldnames so I can see what are common naming conventions. I thought the easiest way for me to replicate this is by creating a new tool so I could use the batch command to go through each selected shapefiles

fc = arcpy.GetParameterAsText(0)

fieldList = arcpy.ListFields(fc)
for field in fieldList:
    fname = field.name
    falias = field.aliasName
    ftype = field.type
    flength = field.length
    print "%s \t %s \t %s \t %i" % (field.name, field.aliasName, field.type, field.length)

How would I then go from here to have that list visible?

When i run this through the tool it says it succeeded but there is no way to view the printed names.

1
  • To store the results for subsequent use, a list of dictionary objects would suffice, or your own class, but these are Python fundamentals and probably off-topic for a GIS-focused question. If you're just concerned with output, the you need to leverage the Messages environment of ArcPy.
    – Vince
    Jan 20 at 11:57

1 Answer 1

5

If you are running this as a tool, use arcpy.AddMessage() instead of print. The print statement does not cause output to be included in the tool's message log.

ie:

arcpy.AddMessage("%s \t %s \t %s \t %i" % (field.name, field.aliasName, field.type, field.length))

The results will be printed in the tool's messsages log.

You may need to expand the to tool window while/after its running to see the output, if it's not already expanded.

Alternatively, you can view the message log for the tool afterwards in the 'Results' pane ('Geoprocessing' menu). Just expand the tool's item, and then either expand the messages, or copy the messages and paste to a text file.

1
  • Brilliant this worked, thank you! Jan 28 at 13:31

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.