Find and Delete all files with .xxx extension

hokiegal99 hokiegal99 at hotmail.com
Sun Dec 14 10:37:38 EST 2003


"Fredrik Lundh" <fredrik at pythonware.com> wrote

> to quote myself from an earlier reply to you:
> 
>     os.path.splitext(fname) splits a filename into prefix and extension
>     parts:
> 
>         >>> os.path.splitext("hello")
>         ('hello', '')
>         >>> os.path.splitext("hello.doc")
>         ('hello', '.doc')
>         >>> os.path.splitext("hello.DOC")
>         ('hello', '.DOC')
>         >>> os.path.splitext("hello.foo")
>         ('hello', '.foo')
> 
> in other words, ext[1] *is* the extension.  

Yes, I know that. You helped me to understand that in a earlier,
different question.


> if you want to look for mp3, MP3, Mp3, etc, you can use the "lower"
> method on the extension:
> 
>         for fname in files:
>             name, ext = os.path.splitext(fname)
>             ext = ext.lower()
>             if ext == ".mp3":
>                 # ... do something with mp3 files ...
>             if ext == ".foo":
>                 # ... do something with foo files ...

Thank you for this example, it's exactly what I was thinking of. I
didn't know that I could use an equivalent comparison to determine
whether or not ext[1] contained the string I wanted to remove, that's
all I was asking.

After reading over the various replies and testing them, I think that
this is the best solution:

if fname.lower().endswith('.mp3'):
    remove...

Thanks again for the examples.




More information about the Python-list mailing list