This Python tutorial explains tempfile.mkstemp() Function: Usage and Examples. Follow the example, inspect the output, and practise an input change so you understand the behavior rather than simply copying the syntax.
What This Feature Does
Create a low-level temporary file with tempfile.mkstemp().
mkstemp() securely creates a temporary file and returns (file_descriptor, absolute_path). Unlike context managers, you must close the descriptor with os.close() and remove the created file yourself.
When to Use It
This technique belongs to file copying, temporary files and matching. It is useful when the input, operation and expected outcome are known. Start with a small controlled example before integrating it into a larger workflow.
Step-by-Step Python Example
Read the code once before running it. Notice which values are inputs, which function or method performs the work, and whether the result is returned or stored by changing an existing object.
import os
import tempfile
file_descriptor, temporary_path = tempfile.mkstemp(suffix=".txt")
try:
os.write(file_descriptor, b"sample")
finally:
os.close(file_descriptor)
os.unlink(temporary_path)
print("Temporary file cleaned up")
Understanding the Code
- First, the imports make os, tempfile available. They provide the filesystem, data or standard-library functions used later.
- Find the call tempfile.mkstemp(suffix=".txt"). Its parameters determine the specific operation demonstrated in the title.
- Its purpose is to create a low-level temporary file with tempfile.mkstemp(). Compare this description against the result printed by the program.
- The try/except section demonstrates how an expected error is handled, rather than allowing the example to stop unexpectedly.
- The print() expressions display a checkable outcome. Print values and exception names are not substitutes for the program result itself.
Expected Output
Temporary file cleaned up
The output is the observable result of the sample operation. Compare its value and type with the explanation, not just whether the program runs.
Important Details and Edge Cases
Prefer NamedTemporaryFile() for most higher-level code.
tempfile.mkstemp() returns an open file descriptor that must be explicitly closed with os.close().
Practice: Try It Yourself
Create a temporary file and clean up the descriptor in finally.
Begin by copying the example into a separate practice file. Change one input at a time, run it again, and describe why the result changed. If the exercise writes or deletes files, keep it inside a temporary test folder.
Official Python Documentation
For the complete reference, see the Python standard library documentation.
Common Beginner Mistakes
- Treating the returned integer descriptor as a high-level file object.
- Failing to close and delete a mkstemp() file, causing leaked resources.
- tempfile.mkstemp() returns an open file descriptor that must be explicitly closed with os.close().
Key Takeaways
- Create a low-level temporary file with tempfile.mkstemp().
- mkstemp() securely creates a temporary file and returns (file_descriptor, absolute_path). Unlike context managers, you must close the descriptor with os.close() and remove the created file yourself.
- Avoid this common pitfall: Treating the returned integer descriptor as a high-level file object.