ericasmi

List Fields doesn't work outside of ArcMap?

Discussion created by ericasmi on Jul 13, 2011
I'm writing a script that will run through multiple mxd files and list the file name, and then the layer names, data sources, and fields. The script runs fine when I run it in the Python window or if I run the script tool in ArcMap while the mxd is open, but if I refer to a different mxd or just run the script in ArcCatalog, it gives IO Errors and says that it can't find the layers. Here's my code:
import arcpy
#set file path for input mxd
fileName = arcpy.GetParameterAsText(0)
arcpy.AddMessage("got file name: " + fileName)
fileNameList = fileName.split(";")

#set file path for output txt file
out = open(arcpy.GetParameterAsText(1), "w")
arcpy.AddMessage("got out file")

#loop through file names
for fName in fileNameList:
  mxd = arcpy.mapping.MapDocument(fName)
  arcpy.AddMessage("got map file: " + mxd.filePath)

  #print mxd file name
  out.write("Mxd file: " + mxd.filePath + '\n')

  #print list of data frames
  for df in arcpy.mapping.ListDataFrames(mxd):
    out.write("Data Frame:" + '\n')
    out.write(df.name + '\n')
    out.write("Layers:" + '\n')
    for lyr in arcpy.mapping.ListLayers(mxd, "*", df):
      out.write(lyr.name + '\n')
      try:
        if lyr.supports("DATASOURCE"):
          out.write(lyr.dataSource + '\n')
        if lyr.isGroupLayer == False:
          fieldList = arcpy.ListFields(lyr)
          for field in fieldList:
            out.write(str(field.name) + ", ")
          out.write ('\n')
      except Exception as e:
        out.write("field error" + '\n')
        out.write(e.message + '\n')
        out.write(str(type(e)) + '\n')
    out.write('\n')
  del mxd

#close output file
out.close()

Any help would be greatly appreciated!

Outcomes