New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
pathlib.Path.exists() on non-existent drive raises WinError instead of returning False #79873
Comments
Tested in 3.7.0 on windows 10. >>> import pathlib
>>> pathlib.Path(r"E:\Whatever\blah.txt").exists() # This drive doesn't exist
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\jordanhu\AppData\Local\Continuum\anaconda2\envs\py3\lib\pathlib.py", line 1318, in exists
self.stat()
File "C:\Users\jordanhu\AppData\Local\Continuum\anaconda2\envs\py3\lib\pathlib.py", line 1140, in stat
return self._accessor.stat(self)
PermissionError: [WinError 21] The device is not ready: 'E:\\Whatever\\blah.txt'
>>> pathlib.Path(r"C:\Whatever\blah.txt").exists() # This drive exists
False |
In bpo-22759 there was some logic applied for which errors to forward rather than hide. I'm inclined to agree that this one should be hidden, but it may have to be done by checking the winerror field rather than the exception type, since other PermissionErrors may mean the file is guaranteed to exist (but you can't touch it) or that the path exists up to the point where you are not allowed to see. I'd happily argue that since these permissions indicate that the file does not exist *for the current user* and so they should be swallowed more broadly, but I'll let Antoine make the call. |
I think exists() should simply return False here. There's no reason a non-existing drive should fail differently than a non-existing parent directory. |
The drive exists (or should) if we're getting ERROR_NOT_READY (21). It's likely a removable media device, such as an optical disc or card reader, and there's no media in the device. If a logical drive isn't defined at all, we should get ERROR_PATH_NOT_FOUND (from the NT status value STATUS_OBJECT_PATH_NOT_FOUND). This gets mapped to the errno value ENOENT, which is already handled. For example: >>> os.stat('Q:/')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
FileNotFoundError: [WinError 3] The system cannot find the path specified: 'Q:/'
>>> pathlib.Path('Q:/whatever/blah.txt').exists()
False Similarly if a UNC 'drive' doesn't exist, we should get ERROR_BAD_NET_NAME (from NT STATUS_BAD_NETWORK_NAME), which is also mapped to ENOENT: >>> os.stat('//some/where')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
FileNotFoundError: [WinError 67] The network name cannot be found: '//some/where'
>>> pathlib.Path('//some/where/whatever/blah.txt').exists()
False |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: