On 19/11/2019 17:57, Andrew Barnert wrote:
On Nov 19, 2019, at 09:40, Rhodri James <rho...@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
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/Z2LE2MIVL22EKK3BZOKHIGAOSG7YTQQS/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to