Tempfile Module: Secure Temporary File Handling
TL;DR
The tempfile module creates secure temporary files and directories with automatic cleanup, preventing race conditions and ensuring cross-platform compatibility for safe temporary data handling.
Interesting!
Tempfile generates cryptographically secure random filenames and automatically selects the best temporary directory location by checking environment variables like TMPDIR, TEMP, and platform-specific defaults.
Basic Temporary Files
TemporaryFile (Memory Efficient)
python code snippet start
import tempfile
# Create temporary file that auto-deletes
with tempfile.TemporaryFile(mode='w+') as tmp:
tmp.write('Hello temporary world!')
tmp.seek(0)
content = tmp.read()
print(content)
# File automatically deleted when context exits
python code snippet end
NamedTemporaryFile (Has Filename)
python code snippet start
import tempfile
import os
# Create named temporary file
with tempfile.NamedTemporaryFile(mode='w+', delete=False) as tmp:
tmp.write('Temporary data')
temp_name = tmp.name
print(f"Temp file: {temp_name}")
# File persists, manual cleanup needed
os.unlink(temp_name)
python code snippet end
Temporary Directories
TemporaryDirectory (Best Practice)
python code snippet start
import tempfile
import os
# Create temporary directory with auto-cleanup
with tempfile.TemporaryDirectory() as temp_dir:
print(f"Temp directory: {temp_dir}")
# Create files in temp directory
temp_file = os.path.join(temp_dir, 'data.txt')
with open(temp_file, 'w') as f:
f.write('Temporary file content')
# Process files...
print(os.listdir(temp_dir))
# Entire directory and contents automatically deleted
python code snippet end
Low-Level Functions
mkstemp and mkdtemp
python code snippet start
import tempfile
import os
# Create temporary file (returns file descriptor)
fd, temp_path = tempfile.mkstemp(suffix='.txt', prefix='myapp_')
try:
with os.fdopen(fd, 'w') as tmp:
tmp.write('Low-level temporary file')
print(f"Created: {temp_path}")
finally:
os.unlink(temp_path) # Manual cleanup required
# Create temporary directory
temp_dir = tempfile.mkdtemp(prefix='myapp_')
try:
print(f"Temp dir: {temp_dir}")
# Use directory...
finally:
import shutil
shutil.rmtree(temp_dir) # Manual cleanup required
python code snippet end
Configuration and Customization
Custom Temporary Locations
python code snippet start
import tempfile
# Set custom temporary directory
old_tmpdir = tempfile.gettempdir()
print(f"Default temp dir: {old_tmpdir}")
# Use custom location
with tempfile.TemporaryDirectory(dir='/path/to/custom/temp') as tmp:
print(f"Custom temp dir: {tmp}")
# Check what directory will be used
print(f"Temp dir: {tempfile.gettempdir()}")
python code snippet end
File Naming Control
python code snippet start
# Control file naming
with tempfile.NamedTemporaryFile(
prefix='myapp_',
suffix='.json',
dir='/tmp'
) as tmp:
print(f"File: {tmp.name}") # e.g., /tmp/myapp_abc123.json
python code snippet end
Practical Examples
Safe File Processing
python code snippet start
import tempfile
import shutil
def process_file_safely(input_file, processor_func):
"""Process file with temporary backup"""
with tempfile.NamedTemporaryFile(mode='w+', delete=False) as tmp:
# Copy original to temp
shutil.copy2(input_file, tmp.name)
try:
# Process original file
result = processor_func(input_file)
return result
except Exception:
# Restore from temp backup
shutil.copy2(tmp.name, input_file)
raise
finally:
# Cleanup temp file
os.unlink(tmp.name)
python code snippet end
Secure Download Processing
python code snippet start
import tempfile
import requests
def download_and_process(url):
"""Download file to temp location for processing"""
with tempfile.NamedTemporaryFile() as tmp:
# Download to temporary file
response = requests.get(url)
tmp.write(response.content)
tmp.flush()
# Process file safely
return process_downloaded_file(tmp.name)
# File automatically cleaned up
python code snippet end
Use tempfile for any temporary data that needs secure, automatic cleanup - it’s essential for robust file handling. Works perfectly with pathlib for path management and os for system operations . Essential for secure inter-process communication and integrates with archive processing workflows .
Reference: Python Tempfile Documentation