Generating Temporary Files and Directories in Python
Python Shorts — Part 6

Developers often need some temporary files or folders, e.g. for unit tests, or just to store any kind of temporary information. For this purpose, they don’t care where these files are placed, how they are named, etc.
One way of achieving this would be create some file / directory manually, e.g. in /tmp, using it, and eventually deleting it manually. This works, but is clunky, you might need to check if the file already exists by coincidence, etc.
Fortunately for us, Python offers some convenience functions in the form of the tempfile module. This e.g. offers the functions TemporaryFile() and TemporaryDirectory(), creating a temporary file / directory, respectively. This is done in a “safe” manner, i.e. uninterrupted, the call makes sure a name is chosen which does not exist yet, places it somewhere “hard-to-find” — and eventually deletes the file / folder when done. This deletion for files is done when the file is closed either explicitly or using a context manager.
Let’s look at one example, one time using an explicit close, and one time using a context manager.
TemporaryFile() with Explicit Close
temp_file = tempfile.TemporaryFile("w+")
temp_file.write("Temp content")
temp_file.seek(0) # Jump back to beginning of file, otherwise read will be empty.
print(temp_file.read())
temp_file.close()TemporaryFile() with Context Manager
import tempfile
with tempfile.TemporaryFile("w+") as temp_file:
temp_file.write("Temp content")
temp_file.seek(0) # Jump back to beginning of file, otherwise read will be empty.
print(temp_file.read())Note that also lower-level functions are available, such as mkstemp() and mkdtemp(), which also generate temporary files / directories, but require manual clean-up — i.e. if not needed for some reason, stick to the higher-level calls as described above.
Thanks for reading!
This post is part of a series show-casing important Python concepts quickly. You can find the other parts here:
- Part 1: Lambda Functions in Python
- Part 2: Iterators in Python
- Part 3: Generators and Generator Expressions in Python
- Part 4: Advanced Iteration in Python with enumerate() and zip()
- Part 5: Managing Resources in Python with Context Managers (with statement)
- Part 7: Logging in Python
- Part 8: Partial Functions in Python
- Part 9: f-Strings in Python
More content at PlainEnglish.io. Sign up for our free weekly newsletter. Join our Discord community and follow us on Twitter, LinkedIn and YouTube.
Learn how to build awareness and adoption for your startup with Circuit.






