Script to print all data layer names and sources?

11205
10
06-13-2011 02:14 PM
847396730
Occasional Contributor III
Hello! In a couple of Esri's python tutorials, they reference the capability of generating a printed document which lists all the data layers in an individual mxd, along with the source of those layers. I would like to do this, but can't find any sample script for it online. Thoughts? Thank you!
Tags (2)
0 Kudos
10 Replies
DarrenWiens2
MVP Honored Contributor
I'm not sure about printing (can't test here), but this will list the data source of each layer (help here) in a mxd (you can just as easily print the layer name):
import arcpy

mxd = arcpy.mapping.MapDocument("MXD PATH HERE")
layers = arcpy.mapping.ListLayers(mxd)

for layer in layers:
    if layer.supports("dataSource"): # some layers might not support the property "dataSource"
        print layer.dataSource
pokateo_
New Contributor III

Thanks Darren! This worked on my first try for me. WOOO!

0 Kudos
RDHarles
Occasional Contributor
Using Darren's code above, simply write each layer.dataSource to a text file for printing like so:

import arcpy

doc = open("doc.txt", "a")

mxd = arcpy.mapping.MapDocument("MXD PATH HERE")
layers = arcpy.mapping.ListLayers(mxd)

for layer in layers:
    if layer.supports("dataSource"): # some layers might not support the property "dataSource"
        print layer.dataSource
        doc.write(layer.dataSource+"\n")
847396730
Occasional Contributor III
Thank you; this is perfect!
0 Kudos
847396730
Occasional Contributor III
Using Darren's code above, simply write each layer.dataSource to a text file for printing like so:

import arcpy

doc = open("doc.txt", "a")

mxd = arcpy.mapping.MapDocument("MXD PATH HERE")
layers = arcpy.mapping.ListLayers(mxd)

for layer in layers:
    if layer.supports("dataSource"): # some layers might not support the property "dataSource"
        print layer.dataSource
        doc.write(layer.dataSource+"\n")


Is it possible for the script to:
a) write this output in a delimited format (the txt is undelimitable, if that's a word, due to the many backslashes in all the path names)
b) create the txt, instead of pouring into an existing txt file

Thanks again!
0 Kudos
DarrenWiens2
MVP Honored Contributor
This code will create and write to a new csv file. Unfortunately, it delimits every character, and I haven't figured out how to fix that. If you do, please post how.

import arcpy, csv

mxd = arcpy.mapping.MapDocument("YOUR MXD HERE")
layers = arcpy.mapping.ListLayers(mxd)
filepath = "NEW CSV FILE PATH HERE"
writer = csv.writer(file(filepath, 'w'))

for layer in layers:
    if layer.supports("dataSource"):
        writer.writerow(layer.dataSource)
del writer


EDIT: this works
import arcpy, csv

mxd = arcpy.mapping.MapDocument("YOUR MXD HERE")
layers = arcpy.mapping.ListLayers(mxd)
filepath = "NEW CSV FILE PATH HERE"
writer = csv.writer(file(filepath, 'wb'))
sourcelist = []

for layer in layers:
    if layer.supports("dataSource"):
        sourcelist.append(layer.dataSource)
writer.writerow(sourcelist)
del writer
0 Kudos
847396730
Occasional Contributor III
Hello! I keep getting this error with the second script: Runtime error <type 'exceptions.IOError'>: [Errno 22] invalid mode ('wb') or filename: ' F:\\MLF\\Special Projects\\District_Maps'

What am I doing wrong?  Thank you!

To clarify: this is the code I entered:

import arcpy, csv
mxd = arcpy.mapping.MapDocument("CURRENT")
layers = arcpy.mapping.ListLayers(mxd)
filepath = " F:\MLF\Special Projects\District_Maps"
writer = csv.writer(file(filepath, 'wb'))
sourcelist = []
for layer in layers:
    if layer.supports("dataSource"):
        sourcelist.append(layer.dataSource)
writer.writerow(sourcelist)
del writer
0 Kudos
DarrenWiens2
MVP Honored Contributor
Try taking out the space before your filename. Other than that, I think the 'wb' option might depend on your version of python, and possibly operating system. I'm using 2.6.5 on Windows 2008, but actually I was going off examples from 2.7. I see posts from 2.6 users who say to use 'w', so maybe try that.
0 Kudos
EricPotvin
New Contributor
I am looking for a way to list all .mxd files in a directory "P:\data\" that have certain .shp file data layer in them.  I have about 1400 .mxd files in my "P:\data\many subfolders\many subfolders".  I would like to find which of the 1400 .mxd files have a special.shp file data layer in the .mxd file.  I need to know which .mxd files have the special.shp data layer in the whole directory.  Can this be done and can someone please help me.
0 Kudos