tempfile
Stdlib — OS/FilePython 2.0+Intermediate
Create temporary files and directories that auto-clean up
Quick Info
- Documentation
- Official Docs
- Python Version
- 2.0+
- Dependencies
- None — Python Standard Library
- Install
Included with Python
Learn by Difficulty
Quick Example
python
import tempfile # Create temporary file with tempfile.NamedTemporaryFile(mode="w", suffix=".txt", delete=False) as f: f.write("Temporary data") print(f"Temp file: {f.name}")
The tempfile module is part of Python's standard library. Create temporary files and directories that auto-clean up.
Try in PlaygroundTags
stdlibfile-iotemporarycleanup
Related Items
pathlib
Stdlib — OS/File
Object-oriented filesystem paths (modern replacement for os.path)
os
Stdlib — OS/File
Interface to the OS: env vars, process management, file operations
shutil
Stdlib — OS/File
High-level file operations: copy, move, remove trees, archives
with
Keyword
Wraps a block with a context manager for automatic setup/teardown