On 16/06/17 22:21, Ethan Furman wrote: > On 06/16/2017 10:36 AM, Thomas Jollans wrote: >> On 08/06/17 15:42, Antoine Pietri wrote: >>> Hello everyone! >>> >>> A very common pattern when dealing with temporary files is code like >>> this: >>> >>> with tempfile.TemporaryDirectory() as tmpdir: >>> tmp_path = tmpdir.name >>> >>> os.chmod(tmp_path) >>> os.foobar(tmp_path) >>> open(tmp_path).read(barquux) >> >> Is it? >> >> py> import tempfile >> py> with tempfile.TemporaryDirectory() as tmpdir: >> ... print(tmpdir, type(tmpdir)) >> ... >> /tmp/tmp2kiqzmi9 <class 'str'> >> py> > > Interesting... on 3.4 and 3.5 I get: > > --> import tempfile > > --> tempfile.TemporaryDirectory() > <TemporaryDirectory '/tmp/tmpy32czx2v'> > > --> with tempfile.TemporaryDirectory() as tmpdir: > ... tmpdir > ... > '/tmp/tmpo63icqfe' > > So a <TemporaryDirectory> if used directly, and a <str> if used as a > context manager. I don't have a copy of 3.6 nor the future 3.7 handy, > so maybe it changed there?
No, this is still the same in py37. The point is that Antoine's code example does not work (in any Python). For NamedTemporaryFile, the situation is different: py> import tempfile py> import os.path py> py> tf1 = tempfile.NamedTemporaryFile() py> tf1.name '/tmp/tmpotcmslpp' py> os.path.exists(tf1.name) True py> with tf1 as tf2: ... print(tf2) ... print(tf2 is tf1) ... <tempfile._TemporaryFileWrapper object at 0x7f7327506a18> True py> os.path.exists(tf1.name) False py> I was wondering about this since the objection that we're dealing with a file object and not a path does not apply for TemporaryDirectory - however, if used "the right way" with a context manager the issue simply doesn't exist. -- Thomas _______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/