I am trying to write a code that will loop through a gdb listing all tables, rasters and featureclasses. It must also list all fields and their types in the featureclasses and print them. I can get everything to print properly with the exception of the ListField. Once I started playing with that command it went down hill. I think the problem lies in determining the dataset for the ListField to run through, I do not know how to tell the ListField to target every featureclass in the designated gdb.
import arcpy, os
### Set Workspace
##gdbpath = raw_input ('Enter File Full Pathname to Geodatabase')
##if arcpy.Exists(gdbpath):
## print 'GDB Found'
##else:
## exit('Invalid Pathname')
gdbpath = 'C:\Users\Stanton\Documents\GISProgramming\Database\Database\Corvallis.gdb'
arcpy.env.workspace = gdbpath
basepath = os.path.basename(gdbpath)
dirpath = os.path.dirname(gdbpath)
print '='*60
print '='*60
# GDB Name
print 'File Geodatabase Name: ' + basepath
#GDB Directory
print 'File Geodatabase Directory: ' + dirpath
# Number of Feature Classes
fcList = arcpy.ListFeatureClasses()
numberFC = len(fcList)
print 'Number of FeatureClasses: ' + str(numberFC)
# Number of Tables
tableList = arcpy.ListTables()
numberTables = len(tableList)
print 'Number of Tables: ' + str(numberTables)
# Number of Rasters:
rasterList = arcpy.ListRasters()
numberRasters = len (rasterList)
print 'Number of Rasters: ' + str(numberRasters)
print '='*60
print '='*60
#Table Names
tableName = tableList
if not tableName:
print 'Tables:'
print '\tNone\n'
else:
print 'Tables:'
for fc in tableName:
print '\t' + fc + '\n'
#Raster Names
rasterName = rasterList
if not rasterName:
print 'Rasters:'
print '\tNone\n'
else:
print 'Rasters:'
for fc in rasterName:
print '\t' + fc + '\n'
#FeaturClassNames and Field Names
fcName = fcList
if not fcName:
print 'FeatureClasses:'
print '\tNone\n'
else:
print 'FeatureClasses:'
dataset = gdbpath
fieldName = arcpy.ListFields(dataset,'*','All')
for fc in fcList:
print '\t' + fc + fieldName
Solved! Go to Solution.
still no an ArcMap...but your last 2 lines need to be indented so that it will cycle through the fieldnames
for fc in fcNamd
fieldName...
for field in
I think
You have to use list fields on each featureclass not on a path as you currently have it. It should be inside your last loop. check the syntax in the list fields topic ArcGIS Help (10.2, 10.2.1, and 10.2.2)
Dan,
Thank you for the reply. I understand that much but what I don't understand is how such a code would be written. I have little to no coding experience. How would I construct a code that would use the listfields on each individual featureclass in any designated gdb? Below is what I have written now with no success still.
#FeaturClassNames and Field Names
fcName = fcList
dataset = fc in gdbpath
fieldName = arcpy.ListFields(dataset,'*','ALL')
for field in fieldName:
fieldList = ("{0} ({1})".format(field.name, field.type))
if not fcName:
print 'FeatureClasses:'
print '\tNone\n'
else:
print 'FeatureClasses:'
for fc in fcList:
print '\t' + fc + fieldList
Your code is still the same...you have to get a cycle through the featureclasses in the geodatabase, then for each featureclass, get a list of its fields, then for each field ... do something, like get its name etc.... in pseudocode since I don't have arcmap on this machine
featureclass_list....get a list of the feature classes
for each featureclass in featureclass_list:
list_fields in featureclass
for each field in list_fields
...do something
Thanks Dan, that helped with my understanding of the problem. Still having trouble with the output.
#FeaturClassNames and Field Names
fcName = fcList
if not fcName:
print 'FeatureClasses:'
print '\tNone\n'
else:
print 'FeatureClasses:'
for fc in fcName:
fieldName = arcpy.ListFields(fc)
for field in fieldName:
print '\t' + str(fcName) + str((" {0} ({1})".format(field.name, field.type)))
and the result is
FeatureClasses:
[u'Address', u'Building', u'BoundaryLine', u'BoundaryPoly', u'Hydrant', u'Manhole', u'MapGrid', u'Parcel', u'ParkingMeters', u'Parks', u'Railroad', u'River', u'Schools', u'Sewer', u'StLights', u'WaterMeter', u'StPaved', u'Hydrant_buff', u'poop_buff', u'poop2_buff'] OBJECTID (OID)
and so on for each type of field.
still no an ArcMap...but your last 2 lines need to be indented so that it will cycle through the fieldnames
for fc in fcNamd
fieldName...
for field in
I think
Dan is correct about the indentation issue when looping through the field names. You want to output each field name for each feature class so you need to nest the fields loop within the feature class loop.
This partial example adds all of the field names to a python list and then outputs the feature class name followed by the fields:
fcName = fcList if not fcName: print 'FeatureClasses:' print '\tNone\n' else: print 'FeatureClasses:' for fc in fcName: fieldNames = arcpy.ListFields(fc) fieldList = [] for field in fieldNames: fieldList.append(str((" {0} ({1})".format(field.name, field.type)))) print str(fc) + ": " + ', '.join(fieldList)
The output looks like this:
FeatureClasses:
Richmond: OBJECTID (OID), RIVER_BASIN_NAME (String), SHAPE (Geometry), SHAPE_Length (Double), SHAPE_Area (Double)
Richmond_Strahler: OBJECTID (OID), SHAPE (Geometry), TOPOID (Integer), CLASSSUBTYPE (Integer), HYDRONAME (String), HYDRONAMETYPE (String), PERENNIALITY (SmallInteger), HIERARCHY (SmallInteger), HYDROTYPE (SmallInteger), CMA (String), COMMENTS (String), SUBCMA (String), STRAHLER (SmallInteger), SHAPE_Length (Double), SomeDate (Date)
Dan and Owen thanks for the help!
The indentation made it run and from there I just had to play with the print statement to get it in the right format.