[Python-ideas] `to_file()` method for strings
Nick Eubank
nickeubank at gmail.com
Tue Mar 22 23:06:48 EDT 2016
As a social scientists trying to help other social scientists move from
language like R, Stata, and Matlab into Python, one of the behaviors I've
found unnecessarily difficult to explain is the "file.open()/file.close()"
idiom (or, alternatively, context managers). In normal operating systems,
and many high level languages, saving is a one-step operation.
I understand there are situations where an open file handle is useful, but
it seems a simple `to_file` method on strings (essentially wrapping a
context-manager) would be really nice, as it would save users from learning
this idiom.
Apparently there's something like this in the Path library (
https://docs.python.org/dev/library/pathlib.html#pathlib.Path.write_text) ,
but I suspect most people have no idea about that method (I've been doing
python for years and this has always been a personal frustration, and I've
asked several others for better options and no one had one to offer), and
it seems like it would make much more sense as a string method. If someone
has a string they want to save to disk, I can't imagine anyone looking in
the Path library.
I respect the desire to avoid bloat -- the context manager or open/close
idiom has just felt unnecessarily complicated (dare I say unpythonic?) for
a common task.
Thanks!
Nick
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20160323/e36b5445/attachment.html>
More information about the Python-ideas
mailing list