AnsweredAssumed Answered

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

Question asked by octaffle on Apr 16, 2015
Latest reply on Apr 16, 2015 by octaffle

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

Outcomes