Python Case Insensitive Glob

By Tyler on

Python 2’s glob by itself is limited in its pattern matching capability but when combined with regex it becomes more useful. Surprisingly, I couldn’t find anything concise and practical for selecting files with certain extensions in a case-insensitive manner so I wrote this:

#!/usr/bin/env python

import glob, re

for file in [f for f in glob.glob('*') if re.match('^.*\.zip$', f, flags=re.IGNORECASE)]:
    print file