For the second problem, I contacted the author (as suggested by DWynne) who gave the correction:
for filename in dirpath[2]:
That corrected the problem. The author told me he contacted Esri shortly after this made it into print but Esri hasn't changed it on the web. He told me he'd contact them again.
For the first problem of this script not listing layers in a group layer, this ought to fix that. Where the code says:
for layer in arcpy.mapping.ListLayers(mxd):
I added after it (sorry, I know the indenting isn't correct)
if lyr.supports("DATASOURCE"):
I found this on an Esri Help page. I'll try it out when I'm back at work. I hope this helps others who have run across this issue.
I have tested this with the problem of the script not listing layers within a group. I also expanded it to allow the output CSV to be automatically named based on the file or folder entered. Here is my code; I hope someone can make use of it.
#Import modules...
import arcpy, os, fnmatch, csv
#User input variables...
mxddirectory = arcpy.GetParameterAsText(0)
mxd_single = arcpy.GetParameterAsText(1)
outputcsvlocation = arcpy.GetParameterAsText(2)
#Create an empty list of ArcMap documents to process...
mxd_list=[]
#If an output location for the CSV is specified, use it...
if len(outputcsvlocation) > 0:
outputcsvlocation_2 = outputcsvlocation
#...otherwise if a singe MXD is specified, name the CSV for it, or...
elif len(mxd_single) > 0:
outputcsvlocation_2 = mxd_single.replace(".mxd",".csv")
#...if a folder location is specified, save a CSV called ListLayers.csv in that folder.
else:
outputcsvlocation_2 = mxddirectory + "\ListLayers.csv"
#If a user defined a single mxd, add its path to the list...
if len(mxd_single) > 0:
mxd_list.append(mxd_single)
#Otherwise walk through the input directory, adding paths for each .mxd file found to the list...
else:
for dirpath in os.walk(mxddirectory):
for filename in dirpath[2]:
if fnmatch.fnmatch(filename, "*.mxd"):
mxd_list.append(os.path.join(dirpath[0], filename))
#Iterate the list of mxd paths and gather property values then write to csv file...
if len(mxd_list) > 0:
#Create the csv file...
outputcsv = open(outputcsvlocation_2,"wb")
writer = csv.writer(outputcsv, dialect = 'excel')
#Write a header row to the csv file...
writer.writerow(["mxdpath", "layername", "layerdescription", "layersource"])
#Iterate through the list of ArcMap Documents...
for mxdpath in mxd_list:
mxdname = os.path.split(mxdpath)[1]
try:
mxd = arcpy.mapping.MapDocument(mxdpath)
#Iterate through the ArcMap Document layers...
for layer in arcpy.mapping.ListLayers(mxd):
if layer.supports("DATASOURCE"): #List all layers that have data, skipping group layers.
layerattributes = [mxdpath, layer.longName, layer.description, layer.dataSource]
#Write the attributes to the csv file...
writer.writerow(layerattributes)
except:
arcpy.AddMessage("EXCEPTION: {0}".format(mxdpath))
del mxd
#close the csv file to save it...
outputcsv.close()
#If no ArcMap Documents are in the list, then notify via an error message...
else:
arcpy.AddError("No ArcMap Documents found. Please check your input variables.")
Thanks to all, especially the original code author.
for dirpath, dirnames, filenames in os.walk(mxddirectory):