[Tutor] Case Insensitive Globing
Mats Wichmann
mats at wichmann.us
Sat May 18 09:59:28 EDT 2019
On 5/17/19 8:14 PM, Richard Damon wrote:
> I am working on a program to process some files created by an old
> windows program that created it files with varying case with a python
> program.
>
> Using glob.glob on Windows seems to ignore the case, and find all the
> matching files.
>
> The same directory, running the same program under Mac OS X, which also
> is a case insensitive file system, is only files that match the case of
> the glob, and is missing many of the files that were found under windows.
>
> Is there an easy was to make glob match files as a case insensitive manner?
>
> Or a simple way to do this with something else.
>
> I am trying to do something like:
>
> for file in glob.glob(pattern): processfile(file)
>
here's a little snippet I've used in the past - uses fnmatch to help
translate the pattern into a regular expression:
import os, re, fnmatch
def findfiles(pattern, path='.'):
rule = re.compile(fnmatch.translate(pattern), re.IGNORECASE)
return [name for name in os.listdir(path) if rule.match(name)]
More information about the Tutor
mailing list