Warm tip: This article is reproduced from stackoverflow.com, please click
python python-3.x glob

Why does Path.rglob() returns filenames in lower case if whole name is specified as pattern?

发布于 2020-03-27 10:29:22

I'h have an executable named "MyCamelCase.exe" in the current python script directory and in a subfolder "MyFolder". Additionally, in "MyFolder" there is another executable "DontWannaFindThis.exe". I'd like to find all occurences of "MyCamelCase.exe" in the current directory and all subfolders. Therefore, I'm using Path.rglob(pattern):

from pathlib import Path

if __name__ == '__main__':

    [print(f) for f in Path.cwd().rglob('MyCamelCase.exe')]
    [print(f) for f in Path.cwd().rglob('.\MyCamelCase.exe')]
    [print(f) for f in Path.cwd().rglob('*.exe')]

This code leads to the following output:

D:\PyTesting\mycamelcase.exe 
D:\PyTesting\MyFolder\mycamelcase.exe
D:\PyTesting\mycamelcase.exe
D:\PyTesting\MyFolder\mycamelcase.exe
D:\PyTesting\MyCamelCase.exe
D:\PyTesting\MyFolder\DontWannaFindThis.exe
D:\PyTesting\MyFolder\MyCamelCase.exe

Why does rglob returns a string with only lower case if a provide the full file name and on the other hand return a string containing the original notation when using a pattern with '.*'? Note: The same happens when using Path.glob()

Questioner
airborne
Viewed
131
Edward Minnix 2019-07-03 23:53

This is because all paths on Windows are case insensitive (in fact, before Windows 10 there was no way to make Windows case sensitive). For some reason, when looking for an exact match, pathlib makes the path lowercase in Windows. When it is doing normal globbing with *, it takes whatever the normal representation is from Windows.

The casing not matching in Windows should not matter though, and it will not if the only consumer of the information is the computer itself when it is processing the files.