[GIS] Creating report of all feature classes in geodatabase using ArcPy

arcpyfeature-classfile-geodatabase

I am not getting any response from this script and no error displayed at all

I am writing this script to help me display the feature classes in my geodatabase

Here is my script

import arcpy
arcpy.env.workspace = r"Q:\basemaps\ALTA\FileGeodatabase\AB_IHS_GIS_DATA.gdb"
logfile = open(r"C:\tmp\log.txt ","w")
fcLst = arcpy.ListFeatureClasses()
for fc in fcLst:
     logfile.write(fc + "\n")
     logfile.close()

Best Answer

Make sure the directory C:\tmp exists. If it does not, your third call will fail. I normally use unicode and double slashes for my workspaces.

u"J:\\restored-20101025\\basemaps\\ALTA\\FileGeodatabase\\GEOBASE_GIS_DATA.gdb"

But the big problem is that your logfile.close() is inside your for loop. That needs to be outside the for loop, or else you will close the logfile after the first featureclass name is written. You also have a typo in your variable assignment for fcLst.

import arcpy, os
arcpy.env.workspace = u"J:\\restored-20101025\\basemaps\\ALTA\\FileGeodatabase\\GEOBASE_GIS_DATA.gdb"
if os.path.exists(arcpy.env.workspace):
    if not os.path.isdir(r"C:\tmp"):
        os.mkdir(r"C:\tmp")
    logfile = open(r"C:\tmp\log.txt", "w")
    fcList = arcpy.ListFeatureClasses()
    dsList = arcpy.ListDatasets()
    for fc in fcList:
        logfile.write(fc+"\n")
    for ds in dsList:
        fcList = arcpy.ListFeatureClasses("","All",ds)
        for fc in fcList:
            logfile.write(fc+"\n")
    logfile.close() 
else:
    print("Workspace does not exist.")
Related Question