<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/creating-temporary-files-in-python-3vfnwx7fs" -->

---
title: Creating temporary files in Python | daily.dev
description: Explains how to create temporary files and directories in Python using the tempfile module. Covers NamedTemporaryFile as a context manager, the delete=False...
canonical: https://daily.dev/posts/creating-temporary-files-in-python-3vfnwx7fs
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Creating temporary files in Python | daily.dev
og:description: Explains how to create temporary files and directories in Python using the tempfile module. Covers NamedTemporaryFile as a context manager, the delete=False...
og:url: https://daily.dev/posts/creating-temporary-files-in-python-3vfnwx7fs
og:image: https://api.daily.dev/og/posts/3VfnWX7Fs.png
og:image:alt: Creating temporary files in Python
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Creating temporary files in Python

**[Planet Python](https://daily.dev/sources/planetpython)** · 4 min read · 0 upvotes · 0 comments

## Summary

Explains how to create temporary files and directories in Python using the tempfile module. Covers NamedTemporaryFile as a context manager, the delete=False option and the newer delete_on_close=False parameter (available since Python 3.12) for closing a file without deleting it early. Also covers TemporaryDirectory for temporary directories, and explains why TemporaryFile is usually not what you want since it lacks a real filename (returning a file descriptor number instead), recommending NamedTemporaryFile whenever an actual filename is needed.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://www.pythonmorsels.com/temporary-files>

## Questions this post answers

### How do I keep a NamedTemporaryFile from being deleted before I'm done using its filename in Python 3.12?

Pass delete_on_close=False to NamedTemporaryFile instead of delete=False. This lets you close the file with file.close() so another process or function can open it by name, while the file still gets automatically deleted once the surrounding with block exits, avoiding the manual os.remove cleanup that delete=False requires.

_daily.dev keeps developers current on new Python standard library options like this one._

### Why does tempfile.TemporaryFile give me a number instead of a filename in Python?

TemporaryFile returns a file descriptor number, not a filename, because the file is created without a name on the filesystem as a security measure that prevents other processes from accessing it. If code needs to pass an actual filename to another function or subprocess, use NamedTemporaryFile instead, since it always exposes a real file.name.

_developers debugging file-handling quirks like this find deep dives on daily.dev._

## Similar posts on daily.dev

- [Working With Files in Python Quiz – Real Python](https://daily.dev/posts/working-with-files-in-python-quiz-real-python-4segmnksy) · Real Python · 0 upvotes · 0 comments
- [How to use virtual environments in Python](https://daily.dev/posts/how-to-use-virtual-environments-in-python-cnm4rzle8) · InfoWorld · 0 upvotes · 0 comments
- [Virtual Environment in Python \(Venv\)](https://daily.dev/posts/virtual-environment-in-python-venv--7efouzsbc) · Medium · 0 upvotes · 0 comments

---

Tags: [#python](https://daily.dev/tags/python)

[View this post on daily.dev](https://daily.dev/posts/creating-temporary-files-in-python-3vfnwx7fs)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"Creating temporary files in Python","url":"https://daily.dev/posts/creating-temporary-files-in-python-3vfnwx7fs","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/creating-temporary-files-in-python-3vfnwx7fs"},"datePublished":"2026-09-04T16:48:46.430Z","dateModified":"2026-09-04T20:08:03.277Z","description":"Explains how to create temporary files and directories in Python using the tempfile module. Covers NamedTemporaryFile as a context manager, the delete=False...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/9123bf8a8e8b4d5c9834b358ef44d05c?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/9123bf8a8e8b4d5c9834b358ef44d05c?_a=AQAEuop","isAccessibleForFree":true,"articleSection":"Planet Python","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"Planet Python","logo":"https://media.daily.dev/image/upload/t_logo,f_auto/v1/logos/eac3cc584cb043b5a242d13752bd3d0c","url":"https://daily.dev/sources/planetpython"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/creating-temporary-files-in-python-3vfnwx7fs","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":0},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"python","timeRequired":"PT4M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Planet Python","item":"https://daily.dev/sources/planetpython"},{"@type":"ListItem","position":3,"name":"Creating temporary files in Python"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/creating-temporary-files-in-python-3vfnwx7fs#faq","mainEntity":[{"@type":"Question","name":"How do I keep a NamedTemporaryFile from being deleted before I'm done using its filename in Python 3.12?","acceptedAnswer":{"@type":"Answer","text":"Pass delete_on_close=False to NamedTemporaryFile instead of delete=False. This lets you close the file with file.close() so another process or function can open it by name, while the file still gets automatically deleted once the surrounding with block exits, avoiding the manual os.remove cleanup that delete=False requires. daily.dev keeps developers current on new Python standard library options like this one."}},{"@type":"Question","name":"Why does tempfile.TemporaryFile give me a number instead of a filename in Python?","acceptedAnswer":{"@type":"Answer","text":"TemporaryFile returns a file descriptor number, not a filename, because the file is created without a name on the filesystem as a security measure that prevents other processes from accessing it. If code needs to pass an actual filename to another function or subprocess, use NamedTemporaryFile instead, since it always exposes a real file.name. developers debugging file-handling quirks like this find deep dives on daily.dev."}}]}
```

