python - 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:'
解决方案
推荐阅读
- java - 检查电子邮件是否包含正则表达式
- angular - 我的侧边菜单使用 Angular 在 Ionic4 下覆盖了我主页的 html 内容
- discord.py-rewrite - 如何删除服务器中的每个角色?
- python - Python线程不释放锁
- google-cloud-dataflow - 在 Google DataFlow 中使用 setup.py 时出现 CalledProcessError
- javascript - 使用 React Typescript 的 Froala 编辑器插件
- android - 如何从 Android 应用程序中启动统一游戏?
- r - 替换一个字符串并用另一个创建一列
- python - Python discord bot“找不到命令'ban'”错误
- c# - c# 在通用控制器上使用自定义过滤器属性