This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Directory at `TemporaryDirectory().name` does not exist
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.6, Python 3.5
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Adam Dangoor, martin.panter, serhiy.storchaka
Priority: normal Keywords:

Created on 2017-11-06 12:05 by Adam Dangoor, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (5)
msg305637 - (view) Author: Adam Dangoor (Adam Dangoor) Date: 2017-11-06 12:05
Sample code:

```
import os
from tempfile import TemporaryDirectory

name = TemporaryDirectory().name
print(os.path.exists(name))  # prints False

td = TemporaryDirectory()
name_2 = td.name
print(os.path.exists(name_2))  # prints True
```

Expected behavior: `True` is printed for both print statements.

I have run this example on:

* CPython 3.6.3
* CPython 3.5.3
* pypy 3.5.3

The unexpected behavior occurs on CPython 3.5.3 and CPython 3.6.X but not on pypy.

(bug found with Tim Weidner https://github.com/timaa2k).
msg305638 - (view) Author: Adam Dangoor (Adam Dangoor) Date: 2017-11-06 12:15
> The unexpected behavior occurs on CPython 3.5.3 and CPython 3.6.X but not on pypy.

This suggests that it is something to do with garbage collection. Upon further thought, maybe this is by design, but I still was surprised.
msg305639 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-11-06 12:38
This is by design. The first TemporaryDirectory object is destroyed before the first print statement. For not be surprised use TemporaryDirectory with the "with" statement.

with TemporaryDirectory() as td:
    name = td.name
    print(os.path.exists(name))  # prints True

print(os.path.exists(name))  # prints False
msg305640 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2017-11-06 12:43
The documentation says “On . . . destruction of the temporary directory object the newly created temporary directory and all its contents are removed”. If you had enabled warnings, you may have seen a hint:

$ python -Wdefault -c 'import tempfile; print(tempfile.TemporaryDirectory().name)'
/usr/lib/python3.5/tempfile.py:788: ResourceWarning: Implicitly cleaning up <TemporaryDirectory '/tmp/tmpj6100h57'>
  _warnings.warn(warn_message, ResourceWarning)
/tmp/tmpj6100h57

This is similar in spirit to earlier bug reports where workarounds were added, but to avoid this instance of the problem the string object returned by the “name” attribute would have to hold a reference back to the directory object.

* Issue 23700: iter(NamedTemporaryFile())
* Issue 18879: NamedTemporaryFile().write(...)
msg305644 - (view) Author: Adam Dangoor (Adam Dangoor) Date: 2017-11-06 15:36
Thank you for clearing this up for me.
History
Date User Action Args
2022-04-11 14:58:54adminsetgithub: 76140
2017-11-06 15:36:01Adam Dangoorsetmessages: + msg305644
2017-11-06 12:44:57martin.pantersetnosy: + serhiy.storchaka

resolution: wont fix -> not a bug
stage: resolved
2017-11-06 12:43:25martin.pantersetnosy: + martin.panter, - serhiy.storchaka
messages: + msg305640
resolution: not a bug -> wont fix
stage: resolved -> (no value)
2017-11-06 12:38:04serhiy.storchakasetstatus: open -> closed

nosy: + serhiy.storchaka
messages: + msg305639

resolution: not a bug
stage: resolved
2017-11-06 12:15:25Adam Dangoorsetmessages: + msg305638
2017-11-06 12:05:51Adam Dangoorcreate