Reputation: 4104
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()
Upvotes: 4
Views: 19204
Reputation: 23
As it have already been answered, this is the correct behaviour on Windows. However, if you want to display the full path without discarding the case, the resolve()
method may be useful.
E.g.
from pathlib import Path
if __name__ == '__main__':
[print(f) for f in Path.cwd().rglob('MyCamelCase.exe').resolve()]
[print(f) for f in Path.cwd().rglob('.\MyCamelCase.exe').resolve()]
[print(f) for f in Path.cwd().rglob('*.exe').resolve()]
Note that .resolve()
is normally used to convert relative and/or symlinked paths to absolute ones.
Upvotes: 2
Reputation: 2947
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.
Upvotes: 6