Delete file start with
<p>I want this function to delete files.  It does this correctly, but it <em>also</em> deletes folders, which I do not want.</p>

<p>I also get an error during execution:</p>

<pre><code>Access is denied: 'C:/temp3\\IDB_KKK
</code></pre>

<p>In folder temp3 i have:</p>

<pre><code>IDB_OPP.txt
IDB_KKK - folder
</code></pre>

<p>Code:</p>

<pre><code>def delete_Files_StartWith(Path,Start_With_Key):
    my_dir = Path
    for fname in os.listdir(my_dir):
        if fname.startswith(Start_With_Key):
            os.remove(os.path.join(my_dir, fname))

delete_Files_StartWith("C:/temp3","IDB_")
</code></pre>

<p>To remove a directory and all its contents, <a href="https://docs.python.org/3/library/shutil.html" rel="nofollow">use <code>shutil</code></a>.</p>

<blockquote>
  <p>The shutil module offers a number of high-level operations on files and collections of files.</p>
</blockquote>

<p>Refer to the question <a href="http://stackoverflow.com/a/303225/137650">How do I remove/delete a folder that is not empty with Python?</a></p>

<pre><code>import shutil

..
    if fname.startswith(Start_With_Key):
        shutil.rmtree(os.path.join(my_dir, fname))
</code></pre>

python