[Python-ideas] `to_file()` method for strings

Zachary Ware zachary.ware+pyideas at gmail.com
Fri Mar 25 12:57:47 EDT 2016


On Wed, Mar 23, 2016 at 1:45 AM, Nick Coghlan <ncoghlan at gmail.com> wrote:
> Having some lower level convenience functions in io may make sense
> (specifically io.write_bytes(path, data) and io.write_text(path, data,
> encoding=None, errors=None), but the str builtin definitely isn't the
> right place for the capability.

In this vein, what about just adding io.read() and io.write()
convenience functions that take the same arguments as io.open() (with
an added 'data' parameter for write()) and do the obvious "open file,
return file.read_or_write(), close file"?  This doesn't answer the
atomic write question, but that wasn't in the OP.  The OP's
'string'.to_file('some_file') would just be io.write('string',
'some_file'), and str.from_file('some_file') would be
io.read('some_file').  Granted, both functions are 3 lines of Python,
but they might well be used enough to be worth adding.

-- 
Zach


More information about the Python-ideas mailing list