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

Chris Barker chris.barker at noaa.gov
Thu Mar 24 18:07:30 EDT 2016


> It is a rare situation when you would want to write just a single string
> to a file.
>
>
> I do it all the time in other languages when dealing with smallish files.
> Python's very nice file-object concept, slant toward iterator-based
> processing, and amazingly consistent ecosystem means that the same issues
> don't apply, so I'd rarely do the same thing.
>

also, Python's strings are immutable, so we really don't want to encourage
people to build up a big string in memory anyway.

and what's wrong with:

open(a_path, 'w').write(the_string)

short, simple one-liner.

OK, non cPython garbage collection may leave that file open and dangling,
but we're talking the quicky scripting data analysis type user -- the
script will terminate soon enough.

BTW, numpy does offer one stop ways to save and load arrays to a file,
binary or text -- and that's a lot more useful than a simple string.
especially the reading.

oh, and for reading:

string = open(path).read()

I really don't get the point of all this.

-CHB


-- 

Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris.Barker at noaa.gov
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20160324/3e8af86e/attachment.html>


More information about the Python-ideas mailing list