Nicholas
Nicholas

Reputation: 2668

Is Unix glob style pattern case sensitive in Python's glob module?

I am not familiarized with glob pattern style and based on this tutorial it says glob is case sensitive.

However, when I use the glob module from Python, it seems glob.glob('./M*') and glob.glob('./m*') return the same results, that is, Python's glob is case-insensitive, see below (I print results together to ensure that they are executed in tandem),

enter image description here

How do I differentiate between upper case and lower case? Do I have to resort to string methods for this sake?

Upvotes: 6

Views: 1624

Answers (1)

Goodies
Goodies

Reputation: 4681

From the glob source code, you can see that it uses os.path.lexists which is backended with lstat (falls back to os.path.exists if lstat is not available). glob itself does nothing to alter the case sensitivity. This is determined by the file system on which the file exists.

[aarcher@Arch]: /tmp/test>$ rm -rf * && touch moo
[aarcher@Arch]: /tmp/test>$ python -q
>>> import glob
>>> glob.glob("m*")
['moo']
>>> glob.glob("M*")
[]
>>> 

Upvotes: 5

Related Questions