|
|
Subscribe / Log in / New account

Re: `to_file()` method for strings

From:  Koos Zevenhoven <k7hoven-Re5JQEeQqe8AvxtiuMwx3w-AT-public.gmane.org>
To:  Nick Eubank <nickeubank-Re5JQEeQqe8AvxtiuMwx3w-AT-public.gmane.org>
Subject:  Re: `to_file()` method for strings
Date:  Thu, 24 Mar 2016 17:02:16 +0200
Message-ID:  <CAMiohoiku-fZ6Z7Aeie_9m0Tnv_XZM4i_t5_b4ZXWDPFa36nUw@mail.gmail.com>
Cc:  python-ideas <python-ideas-+ZN9ApsXKcEdnm+yROfE0A-AT-public.gmane.org>

On Wed, Mar 23, 2016 at 5:06 AM, Nick Eubank <nickeubank-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:

[...]

> Apparently there's something like this in the Path library
> (https://docs.python.org/dev/library/pathlib.html#pathlib....) ,
> 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.
>

Maybe you know this, but Path.read_text and Path.write_text (and the
bytes versions) were introduced in Python 3.5, released only half a
year ago, so there has not really been time for their adoption.

 Path("mytextfile.txt").write_text(...)
 Path("mytextfile.txt").read_text()

Besides being very readable and convenient, one of the nice things
about this is that it helps avoid code like

 open("mytextfile.txt").write(text)

which you see all the time, even if the file will be left open for
some time after this in non-ref-counting implementations of Python.

So, as mentioned before by others, all we need is the better interplay
of pathlib with the rest of the stdlib and that people will learn
about it. Personally, I now always use `pathlib` instead of `open`
when I don't need to care about compatibility with old Python
versions. Maybe some day, Path could be in builtins like open is now?

 - Koos
_______________________________________________
Python-ideas mailing list
Python-ideas-+ZN9ApsXKcEdnm+yROfE0A@public.gmane.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/




to post comments


Copyright © 2016, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds