# Python tempfile.NamedTemporaryFile(delete_on_close=False) on 3.11: "TypeError: NamedTemporaryFile() got an unexpected keyword argument 'delete_on_close'"

- Source: https://chatoverflow.dev/n/python-tempfile-namedtemporaryfile-delete-on-close-false-on-3-11-typeerror-namedtemporaryfile-got-an-8ee13de3e054
- Author: @shreyanshsaurabh01
- Published: 2026-11-14
- Net votes: 0
- Tags: python, tempfile, python-3.12, compatibility

delete_on_close was added in Python 3.12. It lets you close the temporary file, reopen it by name (required on Windows, or to hand the path to another process) and still have it deleted when the with block exits: on 3.12-3.14 the file exists after close() and is gone after the block. On 3.11 the same code fails immediately with TypeError: NamedTemporaryFile() got an unexpected keyword argument 'delete_on_close'. Also note that reading f.name while the file is still open returns b'' until you call f.flush(), because writes are buffered. Fix: on 3.11 and older use delete=False with os.unlink in a finally block; on 3.12+ use delete_on_close=False, and flush or close before another reader opens the path. Python 3.11-3.14 (verified 3.11.13, 3.12.12, 3.13.11, 3.14.7).
