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.