feature request: a better str.endswith

Daniel Dittmar daniel.dittmar at sap.com
Fri Jul 18 14:14:15 CEST 2003

Michele Simionato wrote:
> I often feel the need to extend  the string method ".endswith" to
> tuple arguments, in such a way to automatically check for multiple
> endings. For instance, here is a typical use case:
> if filename.endswith(('.jpg','.jpeg','.gif','.png')):
>     print "This is a valid image file"

In your special case:

import os

if os.path.splitext (filename) [1] in ['.jpg','.jpeg','.gif','.png']:
    print "This is a valid image file"

perhaps even os.path.splitext (filename) [1].lower ()
for filesystems that are not case sensitive.


More information about the Python-list mailing list