Instead, list all the names in the directory so that you can be case-sensitive:
def isfile_casesensitive(path): if not os.path.isfile(path): return False
Demo:
>>> import os >>> file = os.path.join(os.environ('TMP'), 'test.txt') >>> open(file, 'w') # touch <open file 'C:\\...\\test.txt', mode 'w' at 0x00000000021951E0> >>> os.path.isfile(path) True >>> os.path.isfile(path.upper()) True >>> def isfile_casesensitive(path): ... if not os.path.isfile(path): return False # exit early ... directory, filename = os.path.split(path) ... return any(f == filename for f in os.listdir(directory)) ... >>> isfile_casesensitive(path) True >>> isfile_casesensitive(path.upper()) False
Martijn pieters
source share