Find and Delete all files with .xxx extension

William Park opengeometry at yahoo.ca
Sun Dec 14 00:02:04 EST 2003


hokiegal99 <hokiegal99 at hotmail.com> wrote:
> 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"

FYI, in shell, you would go
    find . -type f -name '*.mp3' | xargs rm

-- 
William Park, Open Geometry Consulting, <opengeometry at yahoo.ca>
Linux solution for data management and processing. 




More information about the Python-list mailing list