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()
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.
Ok, to bad. I guess I have to live with that and need to find a workaround.It's difficult to explain why I need the exact notation. Later in the script I use the name as an input to a different program which for some reason needs the exact notation.
Actually, you can make Windows case sensitive. But no, it's not case sensitive by default.
@ShadowRanger thanks for the link! I didn't realize they added that feature in Windows 10. I just knew that in earlier versions of Windows you couldn't do that