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

Koos Zevenhoven k7hoven at gmail.com
Thu Mar 24 17:06:59 EDT 2016


On Thu, Mar 24, 2016 at 7:46 PM, Sven R. Kunze <srkunze at mail.de> wrote:
> On 24.03.2016 16:02, Koos Zevenhoven wrote:
>>
>> 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?
>
> How do you handle Path <-> str conversion? Just living with the extra
> conversion step and don't bother with this ugliness?

For me, it's most often str->Path, meaning things like

 Path("file.txt")

or

 directory = Path("/path/to/some/directory")
 with (directory / "file.txt").open() as f:
     #do something with f
 with (directory / "otherfile.txt").open() as f:
     #do something with f

Then, somewhat less often, I need to give a str as an argument to
something. I then need an additional str(...) around the Path object. That
just feels stupid and makes me start wishing Path was a subclass of str,
and/or that Path was a builtin. Or even better, that you could do
p"filename.txt", which would give you a Path string object. Has this been
discussed?

 - Koos
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20160324/980d1ca2/attachment.html>


More information about the Python-ideas mailing list