Using tempfile module in Python

In this world, everything is ephemeral. If you need to create temporary files or directories in Python, check out the tempfile module.
Whether you want to store intermediate results, manage temp data during execution or just test things out, it can help you by abstracting file creation and cleaning-up operations.
In the a quick walk-through below, we're looking at the following functions:
- TemporaryFile : creates an anonymous temporary file
- NameTemporaryFile: creates a named temp file which we can use in multiple contexts
- TemporaryDirectory: creates a temporary directory (in which we also can create temp files).
Notice how we're using the inside context managers (the 'with' block , check out the comments for a quick intro on them). This means the file will be automatically cleaned up upon exiting (default behavior), unless we specify delete=False at creation.
import tempfile
import os
with tempfile.NamedTemporaryFile(mode='w+t') as temp_file:
temp_file.write('Hello world!')
print(f"Temporary file created at {temp_file.name}")
# Temporary file created at /var/folders/0_/00hs4my104l9tl0x2y386b3c0000gn/T/tmp8nqm3qa1
with tempfile.TemporaryFile(mode='w+t') as temp_file:
temp_file.write('Hello world!')
temp_file.seek(0)
content = temp_file.read()
print(f'Content of the file: {content}')
# Content of the file: Hello world!
with tempfile.TemporaryDirectory() as temp_dir:
print(f"Temporary directory created at: {temp_dir}")
temp_file_path = os.path.join(temp_dir, 'tempfile.txt')
with open(temp_file_path, 'w') as temp_file:
temp_file.write("Hello, World!")
print(temp_file.name)
print(f"Temporary file created at {temp_file.name}")
# Temporary directory created at: /var/folders/0_/00hs4my104l9tl0x2y386b3c0000gn/T/tmp4yh5u33m
# Temporary file created at /var/folders/0_/00hs4my104l9tl0x2y386b3c0000gn/T/tmprgxjwmsj/tempfile.txt
Enjoyed this? Here are some related articles you might find useful: