<div dir="ltr"><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;font-size:small"><br></div><br><div class="gmail_quote"><div dir="ltr">On Thu, Aug 2, 2018 at 7:24 AM Thomas Nyberg via Python-ideas <<a href="mailto:python-ideas@python.org">python-ideas@python.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Is it true that Path('file').read_text() cl<div class="gmail_default" style="font-family:arial,helvetica,sans-serif;font-size:small;display:inline"></div>oses the file after the read? <br>
I think that is the sort of functionality that Ken is asking for.<br>
It's not clear to me by your linked documentation that it does. If it <br>
does, maybe that should be made more clear in that linked documentation? <br></blockquote><div> </div><div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;font-size:small">Agreed. Then again, the documentation includes a link to the source at the top and we find (<a href="https://github.com/python/cpython/blob/3.7/Lib/pathlib.py#L1174">https://github.com/python/cpython/blob/3.7/Lib/pathlib.py#L1174</a>)</div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;font-size:small"><br></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;font-size:small">docstring: 

<span style="color:rgb(3,47,98);font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:12px;white-space:pre;background-color:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:initial;float:none;display:inline">Open the file in text mode, read it, and close the file.</span>

</div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;font-size:small"><span style="color:rgb(3,47,98);font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:12px;white-space:pre;background-color:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:initial;float:none;display:inline"><br></span></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;font-size:small"><span style="color:rgb(3,47,98);font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:12px;white-space:pre;background-color:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:initial;float:none;display:inline">Or ...</span></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;font-size:small"><span style="color:rgb(3,47,98);font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:12px;white-space:pre;background-color:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:initial;float:none;display:inline"><br></span></div><div class="gmail_default"><span style="background-color:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:initial;float:none;display:inline"><font color="#032f62" face="SFMono-Regular, Consolas, Liberation Mono, Menlo, Courier, monospace"><span style="font-size:12px;white-space:pre">>>> import pathlib
>>> help(pathlib.Path.read_text)
Help on function read_text in module pathlib:

read_text(self, encoding=None, errors=None)
    Open the file in text mode, read it, and close the file.</span></font><br></span></div><br></div><div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;font-size:small"><br></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;font-size:small">The documentation would be improved if it used the text from the docstring instead of its own one-line description.</div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;font-size:small"><br></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;font-size:small">André Roberge</div><br></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
(Of course, maybe it's written there somewhere and I'm just blind...)<br>
<br>
Cheers,<br>
Thomas<br>
<br>
On 08/02/2018 11:53 AM, Paul Moore wrote:<br>
> On Thu, 2 Aug 2018 at 10:39, Ken Hilton <<a href="mailto:kenlhilton@gmail.com" target="_blank">kenlhilton@gmail.com</a>> wrote:<br>
> <br>
>> With expressions allow using the enter/exit semantics of the with statement inside an expression context. Examples:<br>
>><br>
>>      contents = f.read() with open('file') as f #the most obvious one<br>
>>      multiplecontents = [f.read() with open(name) as f for name in names] #reading multiple files<br>
>><br>
>> I don't know if it's worth making the "as NAME" part of the with mandatory in an expression - is this a valid use case?<br>
>><br>
>>      data = database.selectrows() with threadlock<br>
>><br>
>> Where this would benefit: I think the major use case is `f.read() with open('file') as f`. Previous documentation has suggested `open('file').read()` and rely on garbage collection; as the disadvantages of that became obvious, it transitioned to a method that couldn't be done in an expression:<br>
> <br>
> That use case is satisfied by pathlib:<br>
> <br>
> Path('file').read_text()<br>
> <br>
> see <a href="https://docs.python.org/3.7/library/pathlib.html#pathlib.Path.read_text" rel="noreferrer" target="_blank">https://docs.python.org/3.7/library/pathlib.html#pathlib.Path.read_text</a><br>
> <br>
> Are there any other use cases? I don't see any real advantage here<br>
> other than the non-advantage of being able to write one-liners.<br>
> Paul<br>
> _______________________________________________<br>
> Python-ideas mailing list<br>
> <a href="mailto:Python-ideas@python.org" target="_blank">Python-ideas@python.org</a><br>
> <a href="https://mail.python.org/mailman/listinfo/python-ideas" rel="noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/python-ideas</a><br>
> Code of Conduct: <a href="http://python.org/psf/codeofconduct/" rel="noreferrer" target="_blank">http://python.org/psf/codeofconduct/</a><br>
> <br>
_______________________________________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org" target="_blank">Python-ideas@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-ideas" rel="noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/python-ideas</a><br>
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" rel="noreferrer" target="_blank">http://python.org/psf/codeofconduct/</a><br>
</blockquote></div></div>