[Tutor] Case Insensitive Globing

Alan Gauld alan.gauld at yahoo.co.uk
Sat May 18 06:52:29 EDT 2019


On 18/05/2019 03:14, Richard Damon wrote:

> The same directory, running the same program under Mac OS X, which also
> is a case insensitive file system, 

That is your mistake. Darwin, the core of the MacOS X system
is a version of BSD Unix and like all Unix OS is very much
case sensitive.

Some of the GUI tools in MacOS X may work as if they were case
insensitive (presumably for backwards compatibility to MacOS 9)
but the underlying OS is case sensitive and all the Terminal
type tools and commands, including Python, follow suit.

> Is there an easy was to make glob match files as a case insensitive manner?

Depends on what you mean by easy :-)
You could include both upper and lower case letters in the search:

glob.glob("[aA][mM][iI][xX][eE][dD][nN][aA][mM][eE]")

And you could write a helper function to generate the search strings.

But personally I'd probably just use listdir and compare with
my search string expressed as a regex.

for fname in os.listdir('.'):
    if re.match("aregex", fname)
       ...

Another option might be to use fnmatch against the uppercase
version of the name

for fname in os.listdir('.'):
   if fnmatch(pattern, fname.upper())
     ...

HTH
-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list