Case sensitive path comparison in python

I have to check whether a file is present a particular path in Mac OS X.

There is a file called foo.F90 inside the directory.

But when I do if(os.path.exists(PATH_TO_foo.f90)), it returns true and does not notice that f90 is lower case and the file which exists is upper case F90.

I tried open(PATH_TO_foo.f90, "r"), even this does not work

How do I get around this?

Answer

As some commenters have noted, Python doesn’t really care about case in paths on case-insensitive filesystems, so none of the path comparison or manipulation functions will really do what you need.

However, you can indirectly test this with os.listdir(), which does give you the directory contents with their actual cases. Given that, you can test if the file exists with something like:

'foo.f90' in os.listdir('PATH_TO_DIRECTORY')