Implements a fake file system that mocks the Python file system modules.
Actively maintained. Last release 4 weeks ago, commits in 13 of the last 26 weeks. A safe choice.
pip install pyfakefspyfakefs

pyfakefs implements a fake file system that mocks the Python file system modules.
Using pyfakefs, your tests operate on a fake file system in memory without
touching the real disk. The software under test requires no modification to
work with pyfakefs.
pyfakefs creates a new empty in-memory file system at each test start, which replaces
the real filesystem during the test. Think of pyfakefs as making a per-test temporary
directory, except for an entire file system.
pyfakefs is tested with current versions of Linux, Windows and macOS.
Usage
There are several ways to invoke pyfakefs:
- using the
fsfixture withpytest - deriving from
fake_filesystem_unittest.TestCaseforunittest - using
fake_filesystem_unittest.Patcheras context manager - using the
fake_filesystem_unittest.patchfsdecorator on a single test
Refer to the usage documentation for more information.
Documentation
- Release documentation covers the latest released version
- Development documentation for the current main branch