Find and Delete all files with .xxx extension

hokiegal99 hokiegal99 at hotmail.com
Sat Dec 13 20:49:44 EST 2003


The below code does what I need it to do, but I thought that using 
something like ext = os.path.splitext(fname) and then searching ext[1] 
for '.mp3' would be a much more accurate approach to solving this 
problem. Could someone demonstrate how to search ext[1] for a specific 
string? This script works great for deleting illegal music on user 
machines ;)

Thanks!!!


import os, string
setpath = raw_input("Enter the path: ") #This can be hard coded.
for root, dirs, files in os.walk(setpath, topdown=False):
     for fname in files:
         s = string.find(fname, '.mp3')
         if s >=1:
             fpath = os.path.join(root,fname)
             os.remove(fpath)
             print "Removed", fpath, "\n"





More information about the Python-list mailing list