glob.glob()
is case-sensitive.
Is there any simple way to find files with specific case-insensitive extension names in Python.
The fnmatch module provides more control over pattern matching than the glob module:
>>> import os
>>> from fnmatch import filter
>>> filter(os.listdir('.'), '*.[Pp][Yy]')
You can also use os.listdir() followed by a regular expression match:
>>> import os, re
>>> [filename for filename in os.listdir('.')
if re.search(r'\.py$', filename, re.IGNORECASE)]
This should do the trick:
import os
import glob
def find_case_insensitve(dirname, extensions):
for filename in glob.glob(dirname):
base, ext = os.path.splitext(filename)
if ext.lower() in extensions:
print filename
find_case_insensitve('/home/anthon/Desktop/*', ['.jpeg', '.png', '.jpg'])
Don't forget to specify the list of extensions in lowercase.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With