<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic List Fields doesn't work outside of ArcMap? in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/list-fields-doesn-t-work-outside-of-arcmap/m-p/195398#M14987</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;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:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;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:
&amp;nbsp; mxd = arcpy.mapping.MapDocument(fName)
&amp;nbsp; arcpy.AddMessage("got map file: " + mxd.filePath)

&amp;nbsp; #print mxd file name
&amp;nbsp; out.write("Mxd file: " + mxd.filePath + '\n')

&amp;nbsp; #print list of data frames
&amp;nbsp; for df in arcpy.mapping.ListDataFrames(mxd):
&amp;nbsp;&amp;nbsp;&amp;nbsp; out.write("Data Frame:" + '\n')
&amp;nbsp;&amp;nbsp;&amp;nbsp; out.write(df.name + '\n')
&amp;nbsp;&amp;nbsp;&amp;nbsp; out.write("Layers:" + '\n')
&amp;nbsp;&amp;nbsp;&amp;nbsp; for lyr in arcpy.mapping.ListLayers(mxd, "*", df):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; out.write(lyr.name + '\n')
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; try:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if lyr.supports("DATASOURCE"):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; out.write(lyr.dataSource + '\n')
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if lyr.isGroupLayer == False:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; fieldList = arcpy.ListFields(lyr)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for field in fieldList:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; out.write(str(field.name) + ", ")
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; out.write ('\n')
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; except Exception as e:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; out.write("field error" + '\n')
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; out.write(e.message + '\n')
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; out.write(str(type(e)) + '\n')
&amp;nbsp;&amp;nbsp;&amp;nbsp; out.write('\n')
&amp;nbsp; del mxd

#close output file
out.close()
&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;SPAN&gt;Any help would be greatly appreciated!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 13 Jul 2011 20:25:58 GMT</pubDate>
    <dc:creator>EricaSmith</dc:creator>
    <dc:date>2011-07-13T20:25:58Z</dc:date>
    <item>
      <title>List Fields doesn't work outside of ArcMap?</title>
      <link>https://community.esri.com/t5/python-questions/list-fields-doesn-t-work-outside-of-arcmap/m-p/195398#M14987</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;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:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;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:
&amp;nbsp; mxd = arcpy.mapping.MapDocument(fName)
&amp;nbsp; arcpy.AddMessage("got map file: " + mxd.filePath)

&amp;nbsp; #print mxd file name
&amp;nbsp; out.write("Mxd file: " + mxd.filePath + '\n')

&amp;nbsp; #print list of data frames
&amp;nbsp; for df in arcpy.mapping.ListDataFrames(mxd):
&amp;nbsp;&amp;nbsp;&amp;nbsp; out.write("Data Frame:" + '\n')
&amp;nbsp;&amp;nbsp;&amp;nbsp; out.write(df.name + '\n')
&amp;nbsp;&amp;nbsp;&amp;nbsp; out.write("Layers:" + '\n')
&amp;nbsp;&amp;nbsp;&amp;nbsp; for lyr in arcpy.mapping.ListLayers(mxd, "*", df):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; out.write(lyr.name + '\n')
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; try:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if lyr.supports("DATASOURCE"):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; out.write(lyr.dataSource + '\n')
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if lyr.isGroupLayer == False:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; fieldList = arcpy.ListFields(lyr)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for field in fieldList:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; out.write(str(field.name) + ", ")
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; out.write ('\n')
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; except Exception as e:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; out.write("field error" + '\n')
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; out.write(e.message + '\n')
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; out.write(str(type(e)) + '\n')
&amp;nbsp;&amp;nbsp;&amp;nbsp; out.write('\n')
&amp;nbsp; del mxd

#close output file
out.close()
&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;SPAN&gt;Any help would be greatly appreciated!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 13 Jul 2011 20:25:58 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/list-fields-doesn-t-work-outside-of-arcmap/m-p/195398#M14987</guid>
      <dc:creator>EricaSmith</dc:creator>
      <dc:date>2011-07-13T20:25:58Z</dc:date>
    </item>
  </channel>
</rss>

