<?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 Delete in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/delete/m-p/1039081#M60509</link>
    <description>&lt;P&gt;I would like to delete all txt files in my folder. Can you tell me why my code does not work?&lt;/P&gt;&lt;P&gt;import os,shutil&lt;/P&gt;&lt;P&gt;one = 'C:/1'&lt;BR /&gt;two = os.listdir(one)&lt;/P&gt;&lt;P&gt;number = 0&lt;/P&gt;&lt;P&gt;for aList in two:&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; if os.path.isdir(aList):&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;myDelete = shutil.&lt;SPAN&gt;rmtree&lt;/SPAN&gt;('*txt', os.path.join(one, aList))&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;number += 1&lt;/P&gt;</description>
    <pubDate>Sun, 21 Mar 2021 10:50:10 GMT</pubDate>
    <dc:creator>Mick</dc:creator>
    <dc:date>2021-03-21T10:50:10Z</dc:date>
    <item>
      <title>Delete</title>
      <link>https://community.esri.com/t5/python-questions/delete/m-p/1039081#M60509</link>
      <description>&lt;P&gt;I would like to delete all txt files in my folder. Can you tell me why my code does not work?&lt;/P&gt;&lt;P&gt;import os,shutil&lt;/P&gt;&lt;P&gt;one = 'C:/1'&lt;BR /&gt;two = os.listdir(one)&lt;/P&gt;&lt;P&gt;number = 0&lt;/P&gt;&lt;P&gt;for aList in two:&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; if os.path.isdir(aList):&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;myDelete = shutil.&lt;SPAN&gt;rmtree&lt;/SPAN&gt;('*txt', os.path.join(one, aList))&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;number += 1&lt;/P&gt;</description>
      <pubDate>Sun, 21 Mar 2021 10:50:10 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/delete/m-p/1039081#M60509</guid>
      <dc:creator>Mick</dc:creator>
      <dc:date>2021-03-21T10:50:10Z</dc:date>
    </item>
    <item>
      <title>Re: Delete</title>
      <link>https://community.esri.com/t5/python-questions/delete/m-p/1039082#M60510</link>
      <description>&lt;P&gt;Check the following code&lt;/P&gt;&lt;P&gt;Change the path of the directory as required.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import os
directory = r".\Directory"

files_in_directory = os.listdir(directory)
filtered_files = [file for file in files_in_directory if file.endswith(".txt")]
for file in filtered_files:
	path_to_file = os.path.join(directory, file)
	os.remove(path_to_file)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 21 Mar 2021 11:04:45 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/delete/m-p/1039082#M60510</guid>
      <dc:creator>JayantaPoddar</dc:creator>
      <dc:date>2021-03-21T11:04:45Z</dc:date>
    </item>
    <item>
      <title>Re: Delete</title>
      <link>https://community.esri.com/t5/python-questions/delete/m-p/1039090#M60511</link>
      <description>&lt;P&gt;Jayanta has provided a workable solution, but I will elaborate on why your existing code doesn't work.&amp;nbsp; The shutil.rmtree method is designed to remove entire directory trees, not files within directory trees.&amp;nbsp; As the documentation states, "&lt;EM&gt;path&lt;/EM&gt; must point to a directory", which in your case '*txt' is not a valid directory path.&lt;/P&gt;</description>
      <pubDate>Sun, 21 Mar 2021 13:47:48 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/delete/m-p/1039090#M60511</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2021-03-21T13:47:48Z</dc:date>
    </item>
  </channel>
</rss>

