Find and Delete all files with .xxx extension

Heike C. Zimmerer usenet03q2 at hczim.de
Tue Dec 16 05:53:56 EST 2003


William Park <opengeometry at yahoo.ca> writes:

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

Which will fail if the file name contains any spaces or other special
characters (not too unusual for .mp3 - Files).

- Heike




More information about the Python-list mailing list