[Python-ideas] `pathlib.Path.write` and `pathlib.Path.read`
Chris Angelico
rosuav at gmail.com
Mon Jan 6 01:38:17 CET 2014
On Mon, Jan 6, 2014 at 4:53 AM, Antoine Pitrou <solipsis at pitrou.net> wrote:
> For starters I think "binary" shouldn't be an
> argument: there should be separate methods for reading/writing text and
> binary contents.
For reading, yes. For writing, the type of the 'data' argument should
say whether it's binary or text, without having to be told.
Not sure it belongs in pathlib, though. Here's the naive code to do a
simple translation on a file:
data = open(fn).read()
open(fn,"w").write(data.replace('Q','QU'))
Doesn't use with, will probably work on CPython but risks trampling on
itself in other interpreters. Needs a solution. Will someone who's
told "hey, there's a potential problem in that code" go looking in
pathlib? I'm not sure about that. I'd be thinking about files and
strings, but not about paths. It'd be great as a built-in:
write_file(fn,read_file(fn).replace('Q','QU'))
or in some namespace that screams "Hey look, file I/O", but I can't
imagine looking for it in pathlib.
Now that 'file' isn't a builtin, would it be worth having a file
module that has this sort of thing? Or would that cause too much
confusion?
ChrisA
More information about the Python-ideas
mailing list