Recommend against (or for?) pathlib.Path.open

When working with pathlib.Path objects, I have been recommending the use of open(path) over path.open() and it’s been brought to my attention that this recommendation is at odds with some linter rules, such as ruff’s PTH123 which recommends replacing open(path) with path.open().

I would like to know what folks think about updating the pathlib documentation to recommend one of these over the other (for an official stance on “there should be one and preferably only one obvious way”).

My reasoning for recommending open(path) over path.open() can be summed up as:

  • pathlib was added to Python in Python 3.4 (it was previously a third-party library)
  • The built-in open function did not accept pathlib.Path objects in Python 3.4 or Python 3.5
  • After PEP 519 was accepted, the new __fspath__ was used by the built-in open function (and many other tools) to allow the built-in open function to accept pathlib.Path objects directly
  • Since Python 3.5’s EOL in 2020, pathlib.Path.open has been rendered redundant (now that the built-in open function accepts any Path-like object in all supported Python versions

Personally, I also prefer using the built-in open function because if I see Python code that includes the expression open(args.cf), I know that a file is being opened, but when I see args.cf.open() I may feel the need to find where args.con was defined to make sure I understand what it is.

I would like to see the pathlib documentation take a stance on whether the built-in open function should be preferred over the open method (or the other way around).

Why use open(path):

  • Works on str and anything that is os.PathLike providing good duck typing for any file that exists on disk.

Why use path.open:

  • Works on zipfile.Pathand similar classes that point to “virtual” files. These new classes may not fullyesupport the samekeywordarguments as open or even require different ones.

For functions it’s often more idiomatic in python to pass an open filestream than a filepath anyway, but when you pass a path it probably depends on whether the parameter points to a physical file or could be any object that has the notion of “openable”.

1 Like

I think it should be left to the user’s judgement. And I think the ruff rule is misguided. Unless we plan on deprecating one of the options, or one is demonstrably inferior, either form is a valid choice.

3 Likes