🌐
Stack Overflow
stackoverflow.com › questions › 77399207 › python-script-calling-tempfile-not-working-in-windows-works-in-ubuntu
python script calling tempfile not working in windows, works in Ubuntu - Stack Overflow
def extract_LCR(seq): tmp_LCR = tempfile.NamedTemporaryFile(delete=False, delete_on_close=False) #flag for windows with open(tmp_LCR.name, 'w') as f_LCR: f_LCR.write('>1\n' + str(seq)) tmp_LCR.seek(0) out = subprocess.Popen(['segmasker', '-in', str(tmp_LCR.name)], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
🌐
GitHub
github.com › python › cpython › issues › 136207
tempfile.NamedTemporaryFile wrong argument · Issue #136207 · python/cpython
July 2, 2025 - Documentation the class NamedTemporaryFile from tempfile not accept delete_on_close as argument, but accept delete but obv got TypeError: NamedTemporaryFile() got an unexpected keyword argument 'delete_on_close'
Author   python
🌐
Google Groups
groups.google.com › g › dev-python › c › 1-cmCnOve08
[Python-Dev] NamedTemporaryFile and context managers
This would look like: ```python ... deletes the file [2]. To handle this scenario, my proposal is two-fold: 1) stop using the TEMPFILE OS attribute so the OS doesn't delete the file on close 2) add `.open()` to NamedTemporaryFile A possible side effect of (1) is that temp ...
🌐
GitHub
github.com › python › cpython › issues › 58451
tempfile.NamedTemporaryFile not particularly useful on Windows · Issue #58451 · python/cpython
March 10, 2012 - gh-58451: Add optional delete_on_close parameter to NamedTemporaryFile #97015
Author   python
🌐
Python
bugs.python.org › issue34429
Issue 34429: [doc] On Windows tempfile.TemporaryFile behaves like NamedTemporaryFile - Python tracker
August 18, 2018 - 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 · This issue has been migrated to GitHub: https://github.com/python/cpython/issues/78610
🌐
Django
code.djangoproject.com › ticket › 16569
#16569 (NamedTemporaryFile is missing the delete argument) – Django
On a side note, delete=True is the default: ​http://docs.python.org/library/tempfile#tempfile.NamedTemporaryFile Just don't specify it and you'll be fine.
🌐
Narkive
python-bugs-list.python.narkive.com › ZZrYWsqv › issue23755-tempfile-namedtemporaryfile-should-be-able-to-toggle-delete
[issue23755] tempfile.NamedTemporaryFile should be able to toggle "delete"
Permalink Martin Panter added the comment: I think I have often passed delete=False because of the documented deficiency with Windows (see Issue 14243). Depending on the outcome of that issue, allowing for deletion after close() might be useful too. BTW, monkey-patching __del__() probably won’t work if you only set an attribute on the “f” instance, instead of modifying the class itself.
🌐
Python
bugs.python.org › issue6357
Issue 6357: tempfile.NamedTemporaryFile does not accept the delete= parameter on Windows - Python tracker
June 28, 2009 - Created on 2009-06-28 20:24 by stutzbach, last changed 2022-04-11 14:56 by admin. This issue is now closed.
🌐
Narkive
python-bugs-list.python.narkive.com › DHyqNFN2 › issue6357-tempfile-namedtemporaryfile-does-not-accept-the-delete-parameter-on-windows
[issue6357] tempfile.NamedTemporaryFile does not accept the delete= parameter on Windows
import tempfile tempfile.NamedTemporaryFile('w', delete=True) <open file '<fdopen>', mode 'w' at 0x7ff1e480> ---------- status: pending -> open _______________________________________ Python tracker <report at bugs.python.org> <http://bugs.python.org/issue6357> _______________________________________
Find elsewhere
Top answer
1 of 3
14

It's an incorrect presumption that you have to close the file before other processes can access it. In fact, as you observed, when you call close on the NamedTemporaryFile it deletes the file on disk by default.

With tempfiles as with all of Python, scope and lifetime are all important. That's one of the great things about object orient languages, objects clean up after themselves when they're no longer needed (either deleted or go out of scope). Upon destruction, the tempfile objects free all resources in memory and named files on disk.

Thus, if you want to use the temporary file on disk by using the file system name, the NamedTemporaryFile should be in scope and unclosed. It's not guaranteed across all platforms, but on Unix/Linux the file should be accessible in the file system by other processes. However, the NamedTemporaryFile creates the file to readable and writeable only by the owner (unix permission 0600: -rw-------).

If you want/need to do more of this management and cleanup yourself, you might consider using the lower level function tempfile.mkstemp(). For example,

fd, tempfilename = tempfile.mkstemp()
f = os.fdopen(fd)

In either case be sure you flush the buffers before giving the filename to another process, file.flush().

2 of 3
5

So that you can use it in the with statement as a context manager and you can get the name of the file via the name property. Consider:

with tempfile.NamedTemporaryFile() as tmp_file:
    subprocess.call(['foo', tmp_file.name])
    do_something_with(tmp_file)

This will automatically delete the file when the body of the with statement is exited either normally or by exception.

🌐
Python
docs.python.org › 3 › library › tempfile.html
tempfile — Generate temporary files and directories
Files names used by this module include a string of random characters which allows those files to be securely created in shared temporary directories. To maintain backward compatibility, the argument order is somewhat odd; it is recommended to use keyword arguments for clarity.
🌐
Python Forum
python-forum.io › thread-41931-page-2.html
[Solved] I'm not getting the good type
April 11, 2024 - (Apr-10-2024, 03:01 PM)Gribouillis Wrote: I disapprove the solution that you found in stackoverflow because instead of solving the problem it hides the problem. You still don't know why the file name
🌐
GitHub
github.com › python › cpython › issues › 111783
NamedTemporaryFile() sample code is vulnerable to file squatting · Issue #111783 · python/cpython
November 6, 2023 - with tempfile.NamedTemporaryFile(delete_on_close=False) as fp: fp.write(b'Hello world!') fp.close() # the file is closed, but not removed # open the file again by using its name with open(fp.name, mode='rb') as f: f.read()
Author   python
🌐
Dot Net Perls
dotnetperls.com › tempfile-python
Python - tempfile Example - Dot Net Perls
March 6, 2024 - import tempfile # Step 1: create new temporary file, and do not delete on close. with tempfile.NamedTemporaryFile(delete_on_close=False) as t: # Step 2: write some lines. t.write(b"bird\n") t.write(b"frog\n") t.write(b"dog\n") # Step 3: close the temporary file.
🌐
Python
python-list.python.narkive.com › av4ZcUec › how-secure-are-temp-files-created-via-tempfile-temporaryfile
How secure are temp files created via tempfile.TemporaryFile()?
... your code should not rely on ... file system. ... tempfile.NamedTemporaryFile(...) This function operates exactly as TemporaryFile() does, except that the file is guaranteed to have a visible name in the file system ......
🌐
Python
bugs.python.org › issue29573
Issue 29573: NamedTemporaryFile with delete=True should not fail if file already deleted - Python tracker
February 15, 2017 - 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 · This issue has been migrated to GitHub: https://github.com/python/cpython/issues/73759
🌐
The Mail Archive
mail-archive.com › dev@subversion.apache.org › msg42313.html
Re: One minor release hiccup
April 2, 2022 - When I ran the script to create ... file_object_for > fd = tempfile.NamedTemporaryFile(mode='w+', encoding='UTF-8') > TypeError: NamedTemporaryFile() got an unexpected keyword argument 'encoding' > > I believe what has not happened is the updating of the 1.10.x and > 1.14.x branches ...
🌐
Python.org
discuss.python.org › ideas
tempfile: changing 'delete' property of NamedTemporaryFile after creation - Ideas - Discussions on Python.org
September 12, 2023 - Sometimes, one creates a file that should be persisted somewhere, but only after lots of work and/or sanity checks. For that, it would be useful to be able to create a tempfile.NamedTemporaryFile with the default delete=True, but then, once all the work is done and the file is to be kept, one should be able to do os.rename(f.name, final_location) f.set_delete(False) # this f.close() In fact, since the object one gets from the NamedTemporaryFile() call has a .delete property, one is easily led...