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?
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')
This is something related to your underlying Operating system and not python. For example, in windows the filesystem is case-insensitive while in Linux it is case sensitive. So if I run the same check, as you are running, on a linux based system I won't get true for case insensitive matches -
>>> os.path.exists('F90')
True
>>> os.path.exists('f90')
False # on my linux based OS
Still if you really want to get a solution around this, you can do this -
if 'f90' in os.listdir(os.path.dirname('PATH_TO_foo.f90')):
# do whatever you want to do
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With