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>Do you want to delete files recursively (i.e. including files that live in <em>subdirectories</em> of <code>Path</code>) without deleting those subdirectories themselves?</p>

<pre><code>import os
def delete_Files_StartWith(Path, Start_With_Key):
    for dirPath, subDirs, fileNames in os.walk(Path):
        for fileName in fileNames: # only considers files, not directories
            if fileName.startswith(Start_With_Key):
                os.remove(os.path.join(dirPath, fileName))
</code></pre>

python