Select to view content in your preferred language

Walk Through folders to find MXDs

584
2
06-22-2012 11:45 AM
MaryM
by
Deactivated User
The below works great on MXDs nested directly in a folder, but how do you walk through all the files in folders in the folder?


import arcpy, os
folderPath = r"myfolderpathhere"
for filename in os.listdir(folderPath):
    fullpath = os.path.join(folderPath, filename)
    if os.path.isfile(fullpath):
        basename, extension = os.path.splitext(fullpath)
        if extension.lower() == ".mxd":
            mxd = arcpy.mapping.MapDocument(fullpath)
            mxd.findAndReplaceWorkspacePaths(r"mybeginningoldfoldeepath", r"mybeginningnewfolderpath")
            mxd.save()
del mxd
Tags (2)
0 Kudos
2 Replies
DarrenWiens2
MVP Alum
I think you want os.walk.
0 Kudos
JeffBarrette
Esri Regular Contributor
This is one variation:

import os

path = r"C:\Active\ArcPY\ResourceCenter"

i = 0
for (path, dirs, files) in os.walk(path):
  for file in files:
    if file.endswith(".mxd"):
      print os.path.join(path,file)


Jeff
0 Kudos