2

I want to use this Python script to check my D: for .mxd's and list them in a CSV, as I want to find which MXD is using a particular file, and this would help.

I don't know where to put the file path ect (d:) and what to change to make it work.

In my environment, what do I need to do to make it work?

import os
import csv
import arcpy

def ListMXDSources(path,extension):
  list_dir = []
  CountList = []
  MapList = []
  list_dir = os.listdir(path)
  count = 0
  for paths, dirctory, files in os.walk(path):
    for file in files:
        if file.endswith(.mxd): # eg: '.mxd'
          MapList.append(os.path.join(paths, file))
          print MapList
          for m in MapList:
                count += 1
                mxd = arcpy.mapping.MapDocument(m)
                ## --------- For each map list layers
                for lyr in arcpy.mapping.ListLayers(mxd):
                    with open("ListOfDataSources.csv", 'wb') as csvfile:
                        csvwriter = csv.writer(csvfile)
                        for dirpath, dirnames, filenames in arcpy.da.Walk(MapList):
                            for filename in filenames:
                                desc = arcpy.Describe(os.path.join(dirpath, filename))
                                csvwriter.writerow([desc.catalogPath, desc.name, desc.dataType])


#-------------Use below to call the function----------------#
ListMXDSources(path=r"ROOT FOLDER LOCATION",extension=".mxd")

I get this result

>     ['d:Untitled.mxd']
>     ['d:Untitled.mxd', 'd:arcpy\\ArcPy_and_ArcGIS_Second_Edition_Code\\Chapter_06\\MXDs\\BrokenLinks.mxd']
>     ['d:Untitled.mxd', 'd:arcpy\\ArcPy_and_ArcGIS_Second_Edition_Code\\Chapter_06\\MXDs\\BrokenLinks.mxd',
> 'd:arcpy\\ArcPy_and_ArcGIS_Second_Edition_Code\\Chapter_06\\MXDs\\MapDocument1.mxd']
>     
>     Traceback (most recent call last):
>       File "D:\SearchMxdListCSV (2).py", line 31, in <module>
>         ListMXDSources(path=r"d:",extension=".mxd")
>       File "D:\SearchMxdListCSV (2).py", line 19, in ListMXDSources
>         mxd = arcpy.mapping.MapDocument(m)
>       File "C:\Program Files (x86)\ArcGIS\Desktop10.3\ArcPy\arcpy\arcobjects\mixins.py", line 611,
> in __init__
>         assert (os.path.isfile(mxd) or (mxd.lower() == "current")), gp.getIDMessage(89004, "Invalid MXD filename")
>     AssertionError: Invalid MXD filename.
  • 1
    Are you using python 2 or 3? – Aaron Nov 13 '18 at 3:44
  • im using python 2 – Tbags McGee Nov 13 '18 at 4:02
  • ik keep getting these blanc .csv's – Tbags McGee Nov 13 '18 at 4:43
  • I'm really not sure where you're going with this.. arcpy.da.Walk doesn't walk inside a list, surely you want to do something like ListLayers for each mxd in the list.. btw you need to unindent for m in MapList otherwise it will be executed for each mxd which makes building a list redundant and worse it will execute for each mxd already discovered exponentially increasing the run time and output size. – Michael Stimson Nov 13 '18 at 4:46
  • ok thanks ive un-indented. yes im trying to ListLayers for each mxd in the list. – Tbags McGee Nov 13 '18 at 5:04
1

as you can see in the print of your path, you have a missing \ in your path. Instead of using "D:" as input, you should use "D:\\"

also, as mentioned in the comment, you should modify your loop. First, opening the csv could be done only once, so put it outside of the loops.

with open("ListOfDataSources.csv", 'wb') as csvfile:
    csvwriter = csv.writer(csvfile)

second, you are looping on your MapList inside the walk loop. This loop must be removed. You should directly loop on the layers when you filename test is true for "mxd"

third, you can use describe directly on the layer object, no need to walk again.

for lyr in arcpy.mapping.ListLayers(mxd):
    desc = arcpy.Describe(lyr)
    csvwriter.writerow([desc.catalogPath, desc.name, desc.dataType])

by the way, I would write the mxd name somewhere in my csv because you might have duplicate layers in different mxds. csvwriter.writerow([mxd, desc.catalogPath, desc.name, desc.dataType])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.