Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I'm struggling with deleting an mxd in a for loop. My code is:

    import arcpy, glob, os, datetime, time
    from time import strftime

    for filename3 in glob.glob(os.path.join(folderPath, "*.mxd")):

    fullpath = os.path.join(folderPath, filename3)
    basename, filename2 = os.path.split(fullpath)

    mxd = arcpy.mapping.MapDocument(filename3)

    if os.path.isfile(fullpath):
        basename, filename2 = os.path.split(fullpath)


        (shortname, extension) = os.path.splitext(filename2)

    mxd.saveACopy(draftloc + "\\" + "SentToDraftOn(" + strftime("%d%b%y %Hh%Mm%Ss") + ")"+ "_" + shortname + extension)
    arcpy.Delete_management(mxd)

I'm trying to do some processing on each mxd in a directory and then I want to saveACopy to a new location and then delete the old mxd. I keep getting the following traceback error:

Object: Error in executing tool

I think it's an indentation thing, but I can't seem to figure it out. Can anyone suggest what I am missing?

Thanks, Mike

share|improve this question
add comment

2 Answers

up vote 3 down vote accepted

Delete_management deletes a file on the disk. mxd as you have it is a reference to a map object in Python (which happens to reference the .mxd on the disk you want to delete. Instead you want to delete the path (which you have as filename3).

Instead you should use:

del mxd #deletes the reference in Python
arcpy.Delete_management(filename3) #deletes the file off the disk
share|improve this answer
add comment

The lines after the for loop won't execute since they are indented. if you want to loop through then the subsequent code needs to be fixed

share|improve this answer
    
+1 Yes, but even worse, the lack of indentation after the 'for' statement will cause the program to fail altogether. I would be surprised if it even ran once! I recommend using an IDE like Eclipse, Notepad++ or even or PythonWin (turn on indentation guides and whitespace under the 'View' menu). They can help with indentation 'gotchas'. –  MappaGnosis Apr 11 '12 at 10:51
add comment

Your Answer

 
discard

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.