In the documentation of pathlib, it says
Path.glob(pattern, *, case_sensitive=None)
There is two things I do not understand.
what is the second parameter * ?
I want to use
case_sensitive
option. But when I runsomepath.glob('*.txt',case_sensitive=False)
I got TypeError: glob() got an unexpected keyword argument 'case_sensitive'
How to use case_sensitive
option in pathlib glob?
A lone
*
without a parameter name indicates that everything following is a keyword-only argument. That is, with the declarationWe can pass
pattern
positionally or by-name, but we can only passcase_sensitive
by name. So all of the following are validBut this is not
The Python designers wrote the function in this way to force you to write more readable code. If you were able to write
path.glob('*', True)
, then everyone reading your code (including you in two weeks) would have to go look up what that second parameter is. But with thecase_sensitive=True
syntax, it's immediately self-evident.As for your second question, the documentation specifies
So I suspect you're running a version of Python older than 3.12.