跳到内容

write-whole-file (FURB103)

派生自 refurb 代码检查工具。

此规则不稳定且处于预览状态。使用需要 --preview 标志。

作用

检查使用 openwrite 是否可以用 pathlib 方法替换,例如 Path.write_textPath.write_bytes

为什么这不好?

当将单个字符串写入文件时,使用像 Path.write_textPath.write_bytes 这样的 pathlib 方法,而不是通过 with 语句调用 openwrite 会更简单明了。

示例

with open(filename, "w") as f:
    f.write(contents)

建议改为

from pathlib import Path

Path(filename).write_text(contents)

参考