classification
Title: os.path.exists() gives wrong answer for Windows special files
Type: behavior Stage: resolved
Components: Library (Lib), Windows Versions: Python 3.3, Python 3.4, Python 3.5, Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: eric.snow, ncoghlan, vajrasky
Priority: normal Keywords:

Created on 2014-02-07 12:47 by ncoghlan, last changed 2014-02-07 17:28 by eric.snow. This issue is now closed.

Messages (4)
msg210460 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2014-02-07 12:47
In trying to figure out why my fix for issue 20053 wasn't working on Windows, I eventually traced it back to the fact that "os.path.exists(os.devnull)" returns False on Windows, so pip isn't picking up my config file override in ensurepip, and reads the global default config file anyway.

However, if you do "if exist NUL (echo exists) ELSE (echo missing)" in cmd, it will print "exists", so this looks like a bug in our os.path.exists implementation.

That is currently implemented in genericpath and assumes that a file exists if-and-only-if os.stat(name) doesn't throw an exception.

It turns out this assumption isn't really correct on Windows - 'NUL' and 'CON' (and presumably other special files) can be opened, but trying to do os.stat() on them throws an exception.
msg210461 - (view) Author: Vajrasky Kok (vajrasky) * Date: 2014-02-07 13:01
See also: http://bugs.python.org/issue1311
msg210475 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2014-02-07 14:37
As per issue 1311, the exists returns True <-> stat will work equivalence is deliberate. We'll have to find a different way to resolve issue 20053 on Windows.

Due to the special files on Windows, the only reliable cross-platform way to find out whether or not *open* will work on a given filename is to actually try it.
msg210500 - (view) Author: Eric Snow (eric.snow) * (Python committer) Date: 2014-02-07 17:28
> As per issue 1311, the exists returns True <-> stat will work equivalence
is deliberate. We'll have to find a different way to resolve issue 20053 on
Windows.

I was going t say we should note this design/impl. detail in the
os.path.exists() docs.  However, it already is!  :)
History
Date User Action Args
2014-02-07 17:28:55eric.snowsetnosy: + eric.snow
messages: + msg210500
2014-02-07 14:37:58ncoghlansetstatus: open -> closed
resolution: not a bug
messages: + msg210475

stage: test needed -> resolved
2014-02-07 13:06:56ncoghlanlinkissue20053 dependencies
2014-02-07 13:01:39vajraskysetnosy: + vajrasky
messages: + msg210461
2014-02-07 12:47:34ncoghlancreate