Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm a beginner to Python and am trying to figure out how I can access several mxds in a folder. For example, when I want to use the AnalyzeforMSD function in ArcGIS:

import arcpy
mxd = arcpy.mapping.MapDocument(r"C:\Project\ReadyForMSD.mxd")
analysis = arcpy.mapping.AnalyzeForMSD(mxd)

How can I define a folder with mxds as a path, e.g. C:\Projects\data, and analyze all the mxds in it?

import os, arcpy

directory = r"C:\Project"
 for root, dirs, files in os.walk(directory):
   for myFile in files:
        fileExt = os.path.splitext(myFile)[1]
        if (fileExt == ".mxd"):
            fullPath = os.path.join(root, myFile)
            print myFile

            myMap = arcpy.mapping.MapDocument(fullPath)
            analysis = arcpy.mapping.AnalyzeForMSD(myMap)

 for key in ('messages', 'warnings', 'errors'):
 print "----" + key.upper() + "---"
 vars = analysis[key]
 for ((message, code), layerlist) in vars.iteritems():
print "    ", message, " (CODE %i)" % code
print "       applies to:",
for layer in layerlist:
share|improve this question

Use os.walk to walk through defined directory, and .endwith to find all mxd's within directory/sub directory, see example below:

import arcpy, os

for root, dirs, files in os.walk(r"C:\Project"):

        for f in files:
            if f.endswith(".mxd"):
                mxd = root + '\\' + f
                analysis = arcpy.mapping.AnalyzeForMSD(mxd)
                # may want to append your analysis info to a text file after here
share|improve this answer
Since ArcGIS 10.1 SP1 it may be worth using arcpy.da.Walk in place of os.walk. – PolyGeo May 22 '15 at 2:27

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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