首页 > 解决方案 > pathlib 的 Path("NUL:").resolve() 在 windows 上抛出错误。这是一个错误吗?

问题描述

我正在寻找与 posix/dev/null文件等效的 windows 功能,我发现NUL:

此片段适用于 Windows 操作系统,证明它确实是一个可写文件:Path('NUL:').write_text('abcd')

但是,Path('NUL:').resolve()抛出异常OSError: [WinError 87] The parameter is incorrect: 'NUL:'

这是我应该报告的错误还是预期的行为?IE 我应该将调用包装resolve()在一个try...except?

如果我捕获所有OSError类型,我如何确定它是否是合法错误?

EG 完整的控制台输出:

Python 3.7.1 (v3.7.1:260ec2c36a, Oct 20 2018, 14:57:15) [MSC v.1915 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from pathlib import Path
>>> Path('NUL:')
WindowsPath('NUL:')
>>> Path('NUL:').write_text('abcd')
4
>>> Path('NUL:').resolve()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Program Files\Python37\lib\pathlib.py", line 1134, in resolve
    s = self._flavour.resolve(self, strict=strict)
  File "C:\Program Files\Python37\lib\pathlib.py", line 192, in resolve
    s = self._ext_to_normal(_getfinalpathname(s))
OSError: [WinError 87] The parameter is incorrect: 'NUL:'

标签: pythonwindowspathlibdev-null

解决方案


推荐阅读