<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Help debugging directory deletion code- shutil.rmtree() not giving error or deleting folders in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/help-debugging-directory-deletion-code-shutil/m-p/537865#M42073</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;&lt;STRONG&gt;In case you're interested:&lt;/STRONG&gt; I got the first part of my program to run and give good outputs! Yay! Thanks for all your help, everyone.&amp;nbsp; 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.&amp;nbsp; But first...&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;My problem:&lt;/STRONG&gt; I want to script a code that will delete all the working directories created by my program. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I've got this code:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;SPAN style="color: #808080; font-style: italic;"&gt;# This script is meant to be used to clear the working directories for a particular
&lt;/SPAN&gt;&lt;SPAN style="color: #808080; font-style: italic;"&gt;# map file. The user selects the map file they are trying to use the program with
&lt;/SPAN&gt;&lt;SPAN style="color: #808080; font-style: italic;"&gt;# and this program deletes the working folders bearing the name of the .MXD file.
&lt;/SPAN&gt;&lt;SPAN style="color: #808080; font-style: italic;"&gt;# This is a tool meant to clean the slate in case write errors are encountered during
&lt;/SPAN&gt;&lt;SPAN style="color: #808080; font-style: italic;"&gt;# tool operation.
&lt;/SPAN&gt;&lt;SPAN style="color: #808080; font-style: italic;"&gt;
&lt;/SPAN&gt;&lt;SPAN style="color: #808080; font-style: italic;"&gt;#### _______________IMPORT MODULES_________________###
&lt;/SPAN&gt;&lt;SPAN style="color: #000080; font-weight: bold;"&gt;import &lt;/SPAN&gt;os
&lt;SPAN style="color: #000080; font-weight: bold;"&gt;import &lt;/SPAN&gt;shutil

&lt;SPAN style="color: #808080; font-style: italic;"&gt;### ________________SET ENVIRONMENTS__________________###
&lt;/SPAN&gt;&lt;SPAN style="color: #808080; font-style: italic;"&gt;# Set Map Document
&lt;/SPAN&gt;MapLoc = &lt;SPAN style="color: #008000; font-weight: bold;"&gt;r"C:\Users\Rachael Johnson\Documents\GradWork\GIS\LakeRidge\LRpythontest.mxd"
&lt;/SPAN&gt;&lt;SPAN style="color: #008000; font-weight: bold;"&gt;
&lt;/SPAN&gt;&lt;SPAN style="color: #808080; font-style: italic;"&gt;# Establish paths
&lt;/SPAN&gt;WorkPath = MapLoc[:-&lt;SPAN style="color: #0000ff;"&gt;4&lt;/SPAN&gt;]
path, filename = os.path.split(MapLoc)
GDBpath = MapLoc[:-&lt;SPAN style="color: #0000ff;"&gt;4&lt;/SPAN&gt;] + &lt;SPAN style="color: #008000; font-weight: bold;"&gt;".gdb"
&lt;/SPAN&gt;TabPath = &lt;SPAN style="color: #008000; font-weight: bold;"&gt;r"C:\outputtable" "\\"
&lt;/SPAN&gt;ProjFolder = TabPath + filename[:-&lt;SPAN style="color: #0000ff;"&gt;4&lt;/SPAN&gt;]

&lt;SPAN style="color: #808080; font-style: italic;"&gt;### __________________DELETE PATHS ASSOCIATED WITH MXD___________ ###
&lt;/SPAN&gt;&lt;SPAN style="color: #000080; font-weight: bold;"&gt;print&lt;/SPAN&gt;(&lt;SPAN style="color: #008000; font-weight: bold;"&gt;"Time to delete"&lt;/SPAN&gt;)
&lt;SPAN style="color: #000080; font-weight: bold;"&gt;if &lt;/SPAN&gt;os.path.exists(WorkPath):
&amp;nbsp;&amp;nbsp; &lt;SPAN style="color: #000080; font-weight: bold;"&gt;print&lt;/SPAN&gt;(&lt;SPAN style="color: #008000; font-weight: bold;"&gt;"deleting..."&lt;/SPAN&gt;)
&amp;nbsp;&amp;nbsp; &lt;SPAN style="color: #000080; font-weight: bold;"&gt;try&lt;/SPAN&gt;:
&amp;nbsp; shutil.rmtree(WorkPath)
&amp;nbsp;&amp;nbsp; &lt;SPAN style="color: #000080; font-weight: bold;"&gt;except&lt;/SPAN&gt;:
&amp;nbsp;&amp;nbsp; &lt;SPAN style="color: #000080; font-weight: bold;"&gt;raise
&lt;/SPAN&gt;&lt;SPAN style="color: #000080; font-weight: bold;"&gt;else&lt;/SPAN&gt;:
&amp;nbsp;&amp;nbsp; &lt;SPAN style="color: #000080; font-weight: bold;"&gt;print&lt;/SPAN&gt;(&lt;SPAN style="color: #008000; font-weight: bold;"&gt;"No such path:" &lt;/SPAN&gt;+ WorkPath)
&lt;SPAN style="color: #000080; font-weight: bold;"&gt;print&lt;/SPAN&gt;(&lt;SPAN style="color: #008000; font-weight: bold;"&gt;"Deleted working and scratch folders in the MXD location:" &lt;/SPAN&gt;+ path)
shutil.rmtree(ProjFolder)
&lt;SPAN style="color: #000080; font-weight: bold;"&gt;print&lt;/SPAN&gt;(&lt;SPAN style="color: #008000; font-weight: bold;"&gt;"Deleted the project table output folder in" &lt;/SPAN&gt;+ TabPath)
shutil.rmtree(GDBpath)
&lt;SPAN style="color: #000080; font-weight: bold;"&gt;print&lt;/SPAN&gt;(&lt;SPAN style="color: #008000; font-weight: bold;"&gt;"Deleted the project geodatabase in the MXD location:" &lt;/SPAN&gt;+ path)&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I've also tried this code without the if statement--just &lt;EM&gt;shutil.rmtree(path)-- &lt;/EM&gt;and I have the same problem.&amp;nbsp; I even tried "ignore_errors=True".&amp;nbsp; Same thing.&amp;nbsp; I have two more directories I'd like to remove but I guess I should get just one working first.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I run this code and don't get any errors but the folder deletion doesn't happen either.&amp;nbsp; 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??&amp;nbsp; I'm at a loss for what to do.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;EDIT: I am running Windows 8.1.&amp;nbsp; My code is in Python 2.7 or whatever version is currently used for ArcGIS 10.3.&amp;nbsp; I am running my code from PyCharm Professional v 4.0.6&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sat, 11 Dec 2021 23:21:08 GMT</pubDate>
    <dc:creator>RachaelJohnson</dc:creator>
    <dc:date>2021-12-11T23:21:08Z</dc:date>
    <item>
      <title>Help debugging directory deletion code- shutil.rmtree() not giving error or deleting folders</title>
      <link>https://community.esri.com/t5/python-questions/help-debugging-directory-deletion-code-shutil/m-p/537865#M42073</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;&lt;STRONG&gt;In case you're interested:&lt;/STRONG&gt; I got the first part of my program to run and give good outputs! Yay! Thanks for all your help, everyone.&amp;nbsp; 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.&amp;nbsp; But first...&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;My problem:&lt;/STRONG&gt; I want to script a code that will delete all the working directories created by my program. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I've got this code:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;SPAN style="color: #808080; font-style: italic;"&gt;# This script is meant to be used to clear the working directories for a particular
&lt;/SPAN&gt;&lt;SPAN style="color: #808080; font-style: italic;"&gt;# map file. The user selects the map file they are trying to use the program with
&lt;/SPAN&gt;&lt;SPAN style="color: #808080; font-style: italic;"&gt;# and this program deletes the working folders bearing the name of the .MXD file.
&lt;/SPAN&gt;&lt;SPAN style="color: #808080; font-style: italic;"&gt;# This is a tool meant to clean the slate in case write errors are encountered during
&lt;/SPAN&gt;&lt;SPAN style="color: #808080; font-style: italic;"&gt;# tool operation.
&lt;/SPAN&gt;&lt;SPAN style="color: #808080; font-style: italic;"&gt;
&lt;/SPAN&gt;&lt;SPAN style="color: #808080; font-style: italic;"&gt;#### _______________IMPORT MODULES_________________###
&lt;/SPAN&gt;&lt;SPAN style="color: #000080; font-weight: bold;"&gt;import &lt;/SPAN&gt;os
&lt;SPAN style="color: #000080; font-weight: bold;"&gt;import &lt;/SPAN&gt;shutil

&lt;SPAN style="color: #808080; font-style: italic;"&gt;### ________________SET ENVIRONMENTS__________________###
&lt;/SPAN&gt;&lt;SPAN style="color: #808080; font-style: italic;"&gt;# Set Map Document
&lt;/SPAN&gt;MapLoc = &lt;SPAN style="color: #008000; font-weight: bold;"&gt;r"C:\Users\Rachael Johnson\Documents\GradWork\GIS\LakeRidge\LRpythontest.mxd"
&lt;/SPAN&gt;&lt;SPAN style="color: #008000; font-weight: bold;"&gt;
&lt;/SPAN&gt;&lt;SPAN style="color: #808080; font-style: italic;"&gt;# Establish paths
&lt;/SPAN&gt;WorkPath = MapLoc[:-&lt;SPAN style="color: #0000ff;"&gt;4&lt;/SPAN&gt;]
path, filename = os.path.split(MapLoc)
GDBpath = MapLoc[:-&lt;SPAN style="color: #0000ff;"&gt;4&lt;/SPAN&gt;] + &lt;SPAN style="color: #008000; font-weight: bold;"&gt;".gdb"
&lt;/SPAN&gt;TabPath = &lt;SPAN style="color: #008000; font-weight: bold;"&gt;r"C:\outputtable" "\\"
&lt;/SPAN&gt;ProjFolder = TabPath + filename[:-&lt;SPAN style="color: #0000ff;"&gt;4&lt;/SPAN&gt;]

&lt;SPAN style="color: #808080; font-style: italic;"&gt;### __________________DELETE PATHS ASSOCIATED WITH MXD___________ ###
&lt;/SPAN&gt;&lt;SPAN style="color: #000080; font-weight: bold;"&gt;print&lt;/SPAN&gt;(&lt;SPAN style="color: #008000; font-weight: bold;"&gt;"Time to delete"&lt;/SPAN&gt;)
&lt;SPAN style="color: #000080; font-weight: bold;"&gt;if &lt;/SPAN&gt;os.path.exists(WorkPath):
&amp;nbsp;&amp;nbsp; &lt;SPAN style="color: #000080; font-weight: bold;"&gt;print&lt;/SPAN&gt;(&lt;SPAN style="color: #008000; font-weight: bold;"&gt;"deleting..."&lt;/SPAN&gt;)
&amp;nbsp;&amp;nbsp; &lt;SPAN style="color: #000080; font-weight: bold;"&gt;try&lt;/SPAN&gt;:
&amp;nbsp; shutil.rmtree(WorkPath)
&amp;nbsp;&amp;nbsp; &lt;SPAN style="color: #000080; font-weight: bold;"&gt;except&lt;/SPAN&gt;:
&amp;nbsp;&amp;nbsp; &lt;SPAN style="color: #000080; font-weight: bold;"&gt;raise
&lt;/SPAN&gt;&lt;SPAN style="color: #000080; font-weight: bold;"&gt;else&lt;/SPAN&gt;:
&amp;nbsp;&amp;nbsp; &lt;SPAN style="color: #000080; font-weight: bold;"&gt;print&lt;/SPAN&gt;(&lt;SPAN style="color: #008000; font-weight: bold;"&gt;"No such path:" &lt;/SPAN&gt;+ WorkPath)
&lt;SPAN style="color: #000080; font-weight: bold;"&gt;print&lt;/SPAN&gt;(&lt;SPAN style="color: #008000; font-weight: bold;"&gt;"Deleted working and scratch folders in the MXD location:" &lt;/SPAN&gt;+ path)
shutil.rmtree(ProjFolder)
&lt;SPAN style="color: #000080; font-weight: bold;"&gt;print&lt;/SPAN&gt;(&lt;SPAN style="color: #008000; font-weight: bold;"&gt;"Deleted the project table output folder in" &lt;/SPAN&gt;+ TabPath)
shutil.rmtree(GDBpath)
&lt;SPAN style="color: #000080; font-weight: bold;"&gt;print&lt;/SPAN&gt;(&lt;SPAN style="color: #008000; font-weight: bold;"&gt;"Deleted the project geodatabase in the MXD location:" &lt;/SPAN&gt;+ path)&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I've also tried this code without the if statement--just &lt;EM&gt;shutil.rmtree(path)-- &lt;/EM&gt;and I have the same problem.&amp;nbsp; I even tried "ignore_errors=True".&amp;nbsp; Same thing.&amp;nbsp; I have two more directories I'd like to remove but I guess I should get just one working first.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I run this code and don't get any errors but the folder deletion doesn't happen either.&amp;nbsp; 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??&amp;nbsp; I'm at a loss for what to do.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;EDIT: I am running Windows 8.1.&amp;nbsp; My code is in Python 2.7 or whatever version is currently used for ArcGIS 10.3.&amp;nbsp; I am running my code from PyCharm Professional v 4.0.6&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 23:21:08 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/help-debugging-directory-deletion-code-shutil/m-p/537865#M42073</guid>
      <dc:creator>RachaelJohnson</dc:creator>
      <dc:date>2021-12-11T23:21:08Z</dc:date>
    </item>
    <item>
      <title>Re: Help debugging directory deletion code- shutil.rmtree() not giving error or deleting folders</title>
      <link>https://community.esri.com/t5/python-questions/help-debugging-directory-deletion-code-shutil/m-p/537866#M42074</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;It looks like you may have some incorrect indentation in the delete paths section. Here's your same code with the correct indentation:&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;### __________________DELETE PATHS ASSOCIATED WITH MXD___________ ###
print("Time to delete")
if os.path.exists(WorkPath):
&amp;nbsp;&amp;nbsp;&amp;nbsp; print("deleting...")
&amp;nbsp;&amp;nbsp;&amp;nbsp; try:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; shutil.rmtree(WorkPath)
&amp;nbsp;&amp;nbsp;&amp;nbsp; except:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; raise
else:
&amp;nbsp;&amp;nbsp;&amp;nbsp; 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)&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 23:21:10 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/help-debugging-directory-deletion-code-shutil/m-p/537866#M42074</guid>
      <dc:creator>BlakeTerhune</dc:creator>
      <dc:date>2021-12-11T23:21:10Z</dc:date>
    </item>
    <item>
      <title>Re: Help debugging directory deletion code- shutil.rmtree() not giving error or deleting folders</title>
      <link>https://community.esri.com/t5/python-questions/help-debugging-directory-deletion-code-shutil/m-p/537867#M42075</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Oh, it's write in PyCharm but it copied wrong.&amp;nbsp; I'll edit my main post accordingly.&amp;nbsp; It still isn't printing the "Time to delete", is giving me no errors, and is not deleting.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 16 Apr 2015 16:35:37 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/help-debugging-directory-deletion-code-shutil/m-p/537867#M42075</guid>
      <dc:creator>RachaelJohnson</dc:creator>
      <dc:date>2015-04-16T16:35:37Z</dc:date>
    </item>
    <item>
      <title>Re: Help debugging directory deletion code- shutil.rmtree() not giving error or deleting folders</title>
      <link>https://community.esri.com/t5/python-questions/help-debugging-directory-deletion-code-shutil/m-p/537868#M42076</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Nevermind, I'm just going to delete this post...!&amp;nbsp; I was running the wrong code.... &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 16 Apr 2015 16:37:23 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/help-debugging-directory-deletion-code-shutil/m-p/537868#M42076</guid>
      <dc:creator>RachaelJohnson</dc:creator>
      <dc:date>2015-04-16T16:37:23Z</dc:date>
    </item>
  </channel>
</rss>

