Python path.exists() returning False

I'm almost 100% sure you're not sanitizing your input before you check if the path exists. Here's something I ran in my interpreter:

>>> from os.path import exists
>>> exists('dog.png')
True
>>> exists('dog.png\n')
False

Try stripping whitespace on path before you check if it exists.


If you read the Python documentation of os.path.exists(), it says that there are specific cases in which a file or folder exists but os.path.exists() returns false:

Return True if path refers to an existing path or an open file descriptor. Returns False for broken symbolic links. On some platforms, this function may return False if permission is not granted to execute os.stat() on the requested file, even if the path physically exists.


This may not answer your question directly, but you could go with the "try/except" method: Whatever function uses the file should return an exception if the file doesn't exist (especially if it's a built-in function), and you can act accordingly. Then you have no need to check whether or not the file exists yourself. Dangerous? Perhaps, but that depends on what you are actually trying to do.