On 19/11/2019 17:57, Andrew Barnert wrote:
On Nov 19, 2019, at 09:40, Rhodri James <rhodri@kynesim.co.uk> wrote:
How about using Paths as file context managers? Just an idle thought.
Then how do you open a Path for writing, or in binary mode, etc.?
With additional parameters and/or methods, obviously.
Adding a Path.opening method that returns an file-on-enter context manager instead of a file would probably work.
Yes, that might work better. Something like: p1 = pathlib.Path("foo/bar/wombat.baz") with p1.opened_as("wb") as outfile: do_stuff(outfile)
But is that better than an opening function that takes a Path (or any of the other valid arguments to open) as its first argument?
To-may-to, to-mah-to. Personally I think it is better, because users won't be so quick to think "oh, that's a file" and try to use it directly. And no, I don't think that trying to open the file on the first attempt to read or write would be at all a good idea! -- Rhodri James *-* Kynesim Ltd