2

I am tryting to create thumbnails of all the mxd's in a directory but no mater how high I set the value of the "While" loop it gives me an "Index out of range" error...

Also can I get it to look at subdirectories as well?

best,

#Make thumbnails from all mxd's in current directory

import arcpy, glob, os, sys
from arcpy import env
from arcpy import mapping
env.overwriteOutput = True

#path = sys.argv[1] # for using in toolset, use directory parameter
path = os.getcwd() # Script in same directory as files being processed
mxdList = glob.glob(path + "\*.mxd")
env.workspace = path
x=0

print 'Reading mxd files from ' + path

mxd_List = glob.glob('*.mxd')
count_Mapdocs = len(mxd_List)
print mxd_List,count_Mapdocs

while x < 50:
 mapDocs=mxd_List[x]
 mapDoc=arcpy.mapping.MapDocument(mapDocs)
 framesList=arcpy.mapping.ListDataFrames(mapDoc)
 mapDoc.activeView=framesList[x]
 mapDoc.makeThumbnail()
 mapDoc.save()
 print "Created thumbnail for " + mxd_List[x]
 print "x= "+ str(x)
 x=x+1
 print "x updated =" + str(x)

1 Answer 1

1

Your problem is the line

while x < 50:
 mapDocs=mxd_list[x]
 ...

What your are trying to do here is index ([x]) objects that do not exist. For instance your statement:

mxd_List = glob.glob('*.mxd')

Is returning a list of mxd's in a directory, there may be 0 (zero) or many objects in this list. If there is only one, and you are trying to specify 50 then you will get an Index Out of Range exception.

You need to do the following (which I noticed in your code you've already got a value for this:

while x < count_Mapdocs:
  mapDocs=mxd_list[x]
  ...

This will fix your error.

Iterating through a directory structure is a little more involved (at least its a step up in your learning curve). You will import the os library, and use it to specify the directories of your input path:

import arcpy, glob, os, sys
from arcpy import env
from arcpy import mapping
env.overwriteOutput = True
your_path = "c:/"

# Check if you path exists
if os.path.exists(your_path):
  # lists all the contents of your path directories and files
  dir_list = os.listdir(your_path) 

  # iterate through your list
  for dir in dir_list:
    # append the name of the object to your_path
    dir_path = os.path.join(your_path, dir)

    # check to make sure the current list object is a directory
    if os.path.isdir(dir_path):
      print dir_path
      #
      # Your code goes here
      #
      mxd_List = glob.glob(os.path.join(dir_path, '*.mxd'))
      count_Mapdocs = len(mxd_List)
      while x < count_Mapdocs:
        mapDocs=mxd_List[x]
        ...

Hope this helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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