[New-bugs-announce] [issue29573] NamedTemporaryFile with delete=True should not fail if file already deleted

Richard Xia report at bugs.python.org
Wed Feb 15 16:21:19 EST 2017


New submission from Richard Xia:

Here is a very short program to demonstrate what I'm seeing:

>>> import tempfile
>>> import os
>>> with tempfile.NamedTemporaryFile(delete=True) as fp:
...     print(fp.name)
...     os.system('rm {}'.format(fp.name))
/tmp/tmpomw0udc6
0
Traceback (most recent call last):
  File "<stdin>", line 3, in <module>
  File "/usr/local/lib/python3.6/tempfile.py", line 502, in __exit__
    self.close()
  File "/usr/local/lib/python3.6/tempfile.py", line 509, in close
    self._closer.close()
  File "/usr/local/lib/python3.6/tempfile.py", line 446, in close
    unlink(self.name)
FileNotFoundError: [Errno 2] No such file or directory: '/tmp/tmpomw0udc6'


In my specific use case, I am shelling out to another program (binutils' objcopy) and passing the path of the NamedTemporaryFile as the output file. objcopy apparently deletes the output file if it encounters an error, which causes NamedTemporaryFile's exit method to fail when it tries to delete the file.

While I can work around this by using delete=False and manually doing the cleanup on my own, it's less elegant than being able to rely on the normal context manager exit.

----------
components: IO
messages: 287888
nosy: richardxia
priority: normal
severity: normal
status: open
title: NamedTemporaryFile with delete=True should not fail if file already deleted
type: behavior
versions: Python 3.5, Python 3.6

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue29573>
_______________________________________


More information about the New-bugs-announce mailing list