Repository navigation
[Windows] OSError when testing whether pathlib.Path('*') exists #79487
Description
Activity
I'm writing a program taking paths from user input through CLI.
pathis a pathlib.Path().Since Windows doesn't expand asterisks, I check if the path doesn't exist. If so I expand using Path().glob(path).
Unfortunately on Windows, if
path(type: Path) contains asterisks, checkingpath.exists()orpath.is_dir()raises WinError 123.Python 3.7.0 (default, Jun 28 2018, 08:04:48) [MSC v.1912 64 bit (AMD64)] :: Anaconda, Inc. on win32 Type "help", "copyright", "credits" or "license" for more information. >>> from pathlib import Path >>> Path('*').exists() Traceback (most recent call last): File "<stdin>", line 1, in <module> File "C:\Users\jimbo1qaz\Miniconda3\envs\python37\lib\pathlib.py", line 1318, in exists self.stat() File "C:\Users\jimbo1qaz\Miniconda3\envs\python37\lib\pathlib.py", line 1140, in stat return self._accessor.stat(self) OSError: [WinError 123] The filename, directory name, or volume label syntax is incorrect: '*' >>> Path('*').is_dir() Traceback (most recent call last): File "<stdin>", line 1, in <module> File "C:\Users\jimbo1qaz\Miniconda3\envs\python37\lib\pathlib.py", line 1330, in is_dir return S_ISDIR(self.stat().st_mode) File "C:\Users\jimbo1qaz\Miniconda3\envs\python37\lib\pathlib.py", line 1140, in stat return self._accessor.stat(self) OSError: [WinError 123] The filename, directory name, or volume label syntax is incorrect: '*'
I also reproduced on Miniconda 3.6.6, 3.7.0, and official Python 3.7.1.
According to https://bugs.python.org/issue29827 , os.path.exists() (not Path.exists() ) returns False on any OSError.
-----------------
On Linux, checking paths with null bytes (a less common occurrence) raises a different error:
>>> import pathlib >>> pathlib.Path("\x00").exists() Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/lib/python3.6/pathlib.py", line 1336, in exists self.stat() File "/usr/lib/python3.6/pathlib.py", line 1158, in stat return self._accessor.stat(self) File "/usr/lib/python3.6/pathlib.py", line 387, in wrapped return strfunc(str(pathobj), *args) ValueError: embedded null byte
- added3.7 (EOL)end of lifeend of lifestdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
on Nov 24, 2018 Path.exists should ignore all OSError exceptions, as os.path.exists does. Barring that, I suppose for Windows we could add EINVAL to IGNORED_ERROS.
The problem with ValueError was already addressed in bpo-33721.
Should Path.resolve() also avoid raising OSError?
Path('*').resolve()
Traceback (most recent call last): ...truncated File "<ipython-input-5-4fa2fec5c8b3>", line 1, in <module> Path('*').resolve() File "C:\Users\jimbo1qaz\AppData\Local\Programs\Python\Python37\lib\pathlib.py", line 1134, in resolve s = self._flavour.resolve(self, strict=strict) File "C:\Users\jimbo1qaz\AppData\Local\Programs\Python\Python37\lib\pathlib.py", line 192, in resolve s = self._ext_to_normal(_getfinalpathname(s)) OSError: [WinError 123] The filename, directory name, or volume label syntax is incorrect: '*'
os.path.realpath('"*') Out[8]: 'C:\\Users\\jimbo1qaz\\Dropbox\\encrypted\\code\\corrscope\\"*' os.path.abspath('*"') Out[13]: 'C:\\Users\\jimbo1qaz\\Dropbox\\encrypted\\code\\corrscope\\*"'
(sidenote: what os.path operation does Path.resolve() match? Path('nonexistent').resolve() returns a relative path on Python 3.7.1, whereas Path().resolve() returns an absolute path.)
Pathlib doesn't necessarily directly follow os on its error handling - adding Antoine for comment.
Passing strict=False to resolve() should be able to handle an invalid name like that. If not, I propose that we change it so that it does.
I'm fine with swallowing the error in both exists() and resolve(). We should be careful not to swallow errors too broadly, though. The code paths should be audited to check that EINVAL can't mean something else.
- changed the title
[-]OSError [WinError 123] when testing if pathlib.Path('*') (asterisks) exists[/-][+][Windows] OSError when testing whether pathlib.Path('*') exists[/+]on Mar 27, 2021 - addedtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
on Mar 27, 2021
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: