Select to view content in your preferred language

Help debugging directory deletion code- shutil.rmtree() not giving error or deleting folders

3379
3
Jump to solution
04-16-2015 08:43 AM
RachaelJohnson
Occasional Contributor

In case you're interested: I got the first part of my program to run and give good outputs! Yay! Thanks for all your help, everyone.  It took me a while to write these basic lines of code but I learned a lot in the process and I am eager to apply what I learned to the hard part of my program next.  But first...

My problem: I want to script a code that will delete all the working directories created by my program.

I've got this code:

# This script is meant to be used to clear the working directories for a particular
# map file. The user selects the map file they are trying to use the program with
# and this program deletes the working folders bearing the name of the .MXD file.
# This is a tool meant to clean the slate in case write errors are encountered during
# tool operation.

#### _______________IMPORT MODULES_________________###
import os
import shutil

### ________________SET ENVIRONMENTS__________________###
# Set Map Document
MapLoc = r"C:\Users\Rachael Johnson\Documents\GradWork\GIS\LakeRidge\LRpythontest.mxd"

# Establish paths
WorkPath = MapLoc[:-4]
path, filename = os.path.split(MapLoc)
GDBpath = MapLoc[:-4] + ".gdb"
TabPath = r"C:\outputtable" "\\"
ProjFolder = TabPath + filename[:-4]

### __________________DELETE PATHS ASSOCIATED WITH MXD___________ ###
print("Time to delete")
if os.path.exists(WorkPath):
   print("deleting...")
   try:
  shutil.rmtree(WorkPath)
   except:
   raise
else:
   print("No such path:" + WorkPath)
print("Deleted working and scratch folders in the MXD location:" + path)
shutil.rmtree(ProjFolder)
print("Deleted the project table output folder in" + TabPath)
shutil.rmtree(GDBpath)
print("Deleted the project geodatabase in the MXD location:" + path)

I've also tried this code without the if statement--just shutil.rmtree(path)-- and I have the same problem.  I even tried "ignore_errors=True".  Same thing.  I have two more directories I'd like to remove but I guess I should get just one working first.

I run this code and don't get any errors but the folder deletion doesn't happen either.  I added the if statement and print statements to see where the hang-up was and it doesn't even print "Time to delete", which tells me it isn't even reading the code for some reason??  I'm at a loss for what to do.

EDIT: I am running Windows 8.1.  My code is in Python 2.7 or whatever version is currently used for ArcGIS 10.3.  I am running my code from PyCharm Professional v 4.0.6

0 Kudos
1 Solution

Accepted Solutions
RachaelJohnson
Occasional Contributor

Nevermind, I'm just going to delete this post...!  I was running the wrong code....

View solution in original post

0 Kudos
3 Replies
BlakeTerhune
MVP Regular Contributor

It looks like you may have some incorrect indentation in the delete paths section. Here's your same code with the correct indentation:

### __________________DELETE PATHS ASSOCIATED WITH MXD___________ ###
print("Time to delete")
if os.path.exists(WorkPath):
    print("deleting...")
    try:
        shutil.rmtree(WorkPath)
    except:
        raise
else:
    print("No such path:" + WorkPath)
print("Deleted working and scratch folders in the MXD location:" + path)
shutil.rmtree(ProjFolder)
print("Deleted the project table output folder in" + TabPath)
shutil.rmtree(GDBpath)
print("Deleted the project geodatabase in the MXD location:" + path)
RachaelJohnson
Occasional Contributor

Oh, it's write in PyCharm but it copied wrong.  I'll edit my main post accordingly.  It still isn't printing the "Time to delete", is giving me no errors, and is not deleting.

0 Kudos
RachaelJohnson
Occasional Contributor

Nevermind, I'm just going to delete this post...!  I was running the wrong code....

0 Kudos