pyiv.files
Use-case package for injectable filesystem surfaces.
Groups path I/O, mounts, and archive-as-tree adapters under one purpose —
not a mirror of os / io / pathlib module names.
What Problem Does This Solve?
File APIs are spread across the stdlib and hard to mock without residue.
Inject Filesystem and bind RealFilesystem, MemoryFilesystem,
MountFilesystem, or archive-backed trees.
Real-World Use Cases:
- Unit tests with MemoryFilesystem (no disk)
- Mount a fake /proc via MappingFilesystem + MountFilesystem
- Read assets from zip/tar with the same consumer API
- Architecture:
Filesystem/RealFilesystem/MemoryFilesystemMountFilesystem/MountPointZipFilesystem/TarFilesystemMappingFilesystem(dict-backed virtual tree adapter)
Example
>>> from pyiv.files import MemoryFilesystem, MountFilesystem, MappingFilesystem
>>> root = MemoryFilesystem()
>>> root.write_text("/app/config.txt", "x=1")
>>> fs = MountFilesystem(root)
>>> fs.mount("/proc", MappingFilesystem({"uptime": "0.1"}))
>>> fs.read_text("/app/config.txt")
'x=1'
>>> fs.read_text("/proc/uptime")
'0.1'
Classes
Abstract filesystem for injectable file I/O. |
|
|
Real filesystem implementation using the standard library. |
In-memory filesystem for testing. |
|
|
Path-prefix router over nested |
|
A path prefix bound to a nested filesystem. |
|
Treat a ZIP archive as a |
|
Treat a tar archive as a read-oriented |
|
Expose a string→bytes (or str) mapping as a flat file tree under |
- class pyiv.files.Filesystem[source]
Bases:
ABCAbstract filesystem for injectable file I/O.
Why this exists: Production code that calls
open()/pathlibdirectly is hard to unit-test without touching disk or mocking builtins. Depend onFilesystemand bindRealFilesystemin production andMemoryFilesystemin tests.Example
>>> from pyiv.filesystem import MemoryFilesystem >>> fs: Filesystem = MemoryFilesystem() >>> fs.write_text("notes.txt", "hello") >>> fs.read_text("notes.txt") 'hello'
- abstract copy(src: str | Path, dst: str | Path) None[source]
Copy a file.
- Parameters:
src – Source path
dst – Destination path
- abstract exists(path: str | Path) bool[source]
Check if a path exists.
- Parameters:
path – Path to check
- Returns:
True if path exists, False otherwise
- abstract get_size(path: str | Path) int[source]
Get file size in bytes.
- Parameters:
path – File path
- Returns:
File size in bytes
- abstract glob(pattern: str | Path) Iterator[Path][source]
Glob pattern matching.
- Parameters:
pattern – Glob pattern
- Yields:
Matching paths
- abstract is_dir(path: str | Path) bool[source]
Check if path is a directory.
- Parameters:
path – Path to check
- Returns:
True if path is a directory, False otherwise
- abstract is_file(path: str | Path) bool[source]
Check if path is a file.
- Parameters:
path – Path to check
- Returns:
True if path is a file, False otherwise
- abstract listdir(path: str | Path) Iterator[str][source]
List directory contents.
- Parameters:
path – Directory path
- Yields:
Directory entry names
- abstract mkdir(path: str | Path, parents: bool = False, exist_ok: bool = False) None[source]
Create a directory.
- Parameters:
path – Directory path
parents – Create parent directories if needed
exist_ok – Don’t raise error if directory exists
- abstract move(src: str | Path, dst: str | Path) None[source]
Move/rename a file or directory.
- Parameters:
src – Source path
dst – Destination path
- abstract open(file: str | Path, mode: str = 'r', encoding: str | None = None) TextIO | BinaryIO[source]
Open a file.
- Parameters:
file – File path
mode – File mode (r, w, a, rb, wb, etc.)
encoding – Text encoding (for text modes)
- Returns:
File handle
- abstract read_bytes(path: str | Path) bytes[source]
Read bytes from a file.
- Parameters:
path – File path
- Returns:
File contents as bytes
- abstract read_text(path: str | Path, encoding: str = 'utf-8') str[source]
Read text from a file.
- Parameters:
path – File path
encoding – Text encoding
- Returns:
File contents as string
- abstract rmdir(path: str | Path) None[source]
Remove a directory.
- Parameters:
path – Directory path
- abstract unlink(path: str | Path, missing_ok: bool = False) None[source]
Remove a file.
- Parameters:
path – File path
missing_ok – Don’t raise error if file doesn’t exist
- walk(path: str | Path) Iterator[tuple][source]
Walk directories yielding
(dirpath, dirnames, filenames).Default implementation uses
listdir/is_dir/is_file.
- class pyiv.files.MappingFilesystem(data: Mapping[str, str | bytes] | None = None)[source]
Bases:
FilesystemExpose a string→bytes (or str) mapping as a flat file tree under
/.Why this exists: Pattern for external adapters (DB row as
/procfile) without putting SQL in core. Mount underMountFilesystem.Example
>>> from pyiv.files import MappingFilesystem, MountFilesystem, MemoryFilesystem >>> proc = MappingFilesystem({"uptime": "1.0", "version": "pyiv"}) >>> fs = MountFilesystem(MemoryFilesystem()) >>> fs.mount("/proc", proc) >>> fs.read_text("/proc/uptime") '1.0'
- copy(src: str | Path, dst: str | Path) None[source]
Copy a file.
- Parameters:
src – Source path
dst – Destination path
- exists(path: str | Path) bool[source]
Check if a path exists.
- Parameters:
path – Path to check
- Returns:
True if path exists, False otherwise
- get_size(path: str | Path) int[source]
Get file size in bytes.
- Parameters:
path – File path
- Returns:
File size in bytes
- glob(pattern: str | Path) Iterator[Path][source]
Glob pattern matching.
- Parameters:
pattern – Glob pattern
- Yields:
Matching paths
- is_dir(path: str | Path) bool[source]
Check if path is a directory.
- Parameters:
path – Path to check
- Returns:
True if path is a directory, False otherwise
- is_file(path: str | Path) bool[source]
Check if path is a file.
- Parameters:
path – Path to check
- Returns:
True if path is a file, False otherwise
- listdir(path: str | Path) Iterator[str][source]
List directory contents.
- Parameters:
path – Directory path
- Yields:
Directory entry names
- mkdir(path: str | Path, parents: bool = False, exist_ok: bool = False) None[source]
Create a directory.
- Parameters:
path – Directory path
parents – Create parent directories if needed
exist_ok – Don’t raise error if directory exists
- move(src: str | Path, dst: str | Path) None[source]
Move/rename a file or directory.
- Parameters:
src – Source path
dst – Destination path
- open(file: str | Path, mode: str = 'r', encoding: str | None = None) TextIO | BinaryIO[source]
Open a file.
- Parameters:
file – File path
mode – File mode (r, w, a, rb, wb, etc.)
encoding – Text encoding (for text modes)
- Returns:
File handle
- read_bytes(path: str | Path) bytes[source]
Read bytes from a file.
- Parameters:
path – File path
- Returns:
File contents as bytes
- read_text(path: str | Path, encoding: str = 'utf-8') str[source]
Read text from a file.
- Parameters:
path – File path
encoding – Text encoding
- Returns:
File contents as string
- unlink(path: str | Path, missing_ok: bool = False) None[source]
Remove a file.
- Parameters:
path – File path
missing_ok – Don’t raise error if file doesn’t exist
- class pyiv.files.MemoryFilesystem[source]
Bases:
FilesystemIn-memory filesystem for testing.
Use this whenever code under test needs file I/O but must not touch the real disk. Paths live in a dict-backed store, so tests stay fast and isolated.
Example
>>> from pyiv.files import MemoryFilesystem >>> fs = MemoryFilesystem() >>> fs.write_text("/notes/a.txt", "alpha") >>> fs.mkdir("/notes/sub", parents=True, exist_ok=True) >>> fs.exists("/notes/a.txt") True >>> fs.read_text("/notes/a.txt") 'alpha'
- copy(src: str | Path, dst: str | Path) None[source]
Copy a file.
- Parameters:
src – Source file path
dst – Destination file path
- Raises:
FileNotFoundError – If source file doesn’t exist
- exists(path: str | Path) bool[source]
Check if path exists.
- Parameters:
path – Path to check
- Returns:
True if path exists (as file or directory), False otherwise
- get_size(path: str | Path) int[source]
Get file size in bytes.
- Parameters:
path – File path
- Returns:
File size in bytes
- Raises:
FileNotFoundError – If file doesn’t exist
- glob(pattern: str | Path) Iterator[Path][source]
Glob pattern matching (simplified).
- Parameters:
pattern – Glob pattern to match
- Yields:
Matching paths
- is_dir(path: str | Path) bool[source]
Check if path is a directory.
- Parameters:
path – Path to check
- Returns:
True if path is a directory, False otherwise
- is_file(path: str | Path) bool[source]
Check if path is a file.
- Parameters:
path – Path to check
- Returns:
True if path is a file, False otherwise
- listdir(path: str | Path) Iterator[str][source]
List directory contents.
- Parameters:
path – Directory path to list
- Yields:
Directory entry names
- Raises:
FileNotFoundError – If directory doesn’t exist
- mkdir(path: str | Path, parents: bool = False, exist_ok: bool = False) None[source]
Create a directory.
- Parameters:
path – Directory path to create
parents – Create parent directories if needed
exist_ok – Don’t raise error if directory already exists
- Raises:
FileExistsError – If directory or file exists at path and exist_ok is False
FileNotFoundError – If parent directory doesn’t exist and parents is False
- move(src: str | Path, dst: str | Path) None[source]
Move/rename a file or directory.
- Parameters:
src – Source path
dst – Destination path
- Raises:
FileNotFoundError – If source doesn’t exist
- open(file: str | Path, mode: str = 'r', encoding: str | None = None) TextIO | BinaryIO[source]
Open a file in memory.
- Parameters:
file – File path to open
mode – File mode (r, w, a, rb, wb, etc.)
encoding – Text encoding (for text modes, ignored for binary modes)
- Returns:
File handle (TextIO for text modes, BinaryIO for binary modes)
- Raises:
FileNotFoundError – If file doesn’t exist in read mode
ValueError – If mode is unsupported
- read_bytes(path: str | Path) bytes[source]
Read bytes from a file.
- Parameters:
path – File path to read
- Returns:
File contents as bytes
- Raises:
FileNotFoundError – If file doesn’t exist
- read_text(path: str | Path, encoding: str = 'utf-8') str[source]
Read text from a file.
- Parameters:
path – File path to read
encoding – Text encoding (default: utf-8)
- Returns:
File contents as string
- Raises:
FileNotFoundError – If file doesn’t exist
- rmdir(path: str | Path) None[source]
Remove a directory.
- Parameters:
path – Directory path to remove (must be empty)
- Raises:
FileNotFoundError – If directory doesn’t exist
ValueError – If trying to remove root directory
OSError – If directory is not empty
- unlink(path: str | Path, missing_ok: bool = False) None[source]
Remove a file.
- Parameters:
path – File path to remove
missing_ok – Don’t raise error if file doesn’t exist
- Raises:
FileNotFoundError – If file doesn’t exist and missing_ok is False
- class pyiv.files.MountFilesystem(root: Filesystem | None = None)[source]
Bases:
FilesystemPath-prefix router over nested
Filesysteminstances.Why this exists: Expose virtual trees (fake
/proc, zip mounts, etc.) beside a real or memory root without monkeypatchingopen.Example
>>> from pyiv.files import MemoryFilesystem, MountFilesystem >>> root = MemoryFilesystem() >>> root.write_text("/data/a.txt", "a") >>> proc = MemoryFilesystem() >>> proc.write_text("/status", "ok") >>> fs = MountFilesystem(root) >>> fs.mount("/proc", proc) >>> fs.read_text("/data/a.txt") 'a' >>> fs.read_text("/proc/status") 'ok'
- __init__(root: Filesystem | None = None) None[source]
- copy(src: str | Path, dst: str | Path) None[source]
Copy a file.
- Parameters:
src – Source path
dst – Destination path
- exists(path: str | Path) bool[source]
Check if a path exists.
- Parameters:
path – Path to check
- Returns:
True if path exists, False otherwise
- get_size(path: str | Path) int[source]
Get file size in bytes.
- Parameters:
path – File path
- Returns:
File size in bytes
- glob(pattern: str | Path) Iterator[Path][source]
Glob pattern matching.
- Parameters:
pattern – Glob pattern
- Yields:
Matching paths
- is_dir(path: str | Path) bool[source]
Check if path is a directory.
- Parameters:
path – Path to check
- Returns:
True if path is a directory, False otherwise
- is_file(path: str | Path) bool[source]
Check if path is a file.
- Parameters:
path – Path to check
- Returns:
True if path is a file, False otherwise
- listdir(path: str | Path) Iterator[str][source]
List directory contents.
- Parameters:
path – Directory path
- Yields:
Directory entry names
- mkdir(path: str | Path, parents: bool = False, exist_ok: bool = False) None[source]
Create a directory.
- Parameters:
path – Directory path
parents – Create parent directories if needed
exist_ok – Don’t raise error if directory exists
- mount(mount_path: str | Path, filesystem: Filesystem) None[source]
Mount
filesystematmount_path(longest prefix wins).
- move(src: str | Path, dst: str | Path) None[source]
Move/rename a file or directory.
- Parameters:
src – Source path
dst – Destination path
- open(file: str | Path, mode: str = 'r', encoding: str | None = None) TextIO | BinaryIO[source]
Open a file.
- Parameters:
file – File path
mode – File mode (r, w, a, rb, wb, etc.)
encoding – Text encoding (for text modes)
- Returns:
File handle
- read_bytes(path: str | Path) bytes[source]
Read bytes from a file.
- Parameters:
path – File path
- Returns:
File contents as bytes
- read_text(path: str | Path, encoding: str = 'utf-8') str[source]
Read text from a file.
- Parameters:
path – File path
encoding – Text encoding
- Returns:
File contents as string
- unlink(path: str | Path, missing_ok: bool = False) None[source]
Remove a file.
- Parameters:
path – File path
missing_ok – Don’t raise error if file doesn’t exist
- class pyiv.files.MountPoint(mount_path: str, filesystem: Filesystem)[source]
Bases:
objectA path prefix bound to a nested filesystem.
Why this exists: Frozen knob for mount tables (Settings-friendly).
Example
>>> from pyiv.files import MemoryFilesystem, MountPoint >>> MountPoint("/proc", MemoryFilesystem()).mount_path '/proc'
- __init__(mount_path: str, filesystem: Filesystem) None
- filesystem: Filesystem
- class pyiv.files.RealFilesystem(root: str | Path | None = None)[source]
Bases:
FilesystemReal filesystem implementation using the standard library.
Use this in production when you need actual disk I/O. Prefer
MemoryFilesystemin unit tests so examples and CI never write into the working tree. Optionalroot=chroots all paths under a sandbox.Why this exists: Production Filesystem that performs real disk I/O.
Example
>>> import tempfile >>> from pathlib import Path >>> from pyiv.files import RealFilesystem >>> fs = RealFilesystem() >>> with tempfile.TemporaryDirectory() as d: ... p = Path(d) / "hello.txt" ... fs.write_text(p, "hi") ... fs.read_text(p) 'hi'
- copy(src: str | Path, dst: str | Path) None[source]
Copy a file.
- Parameters:
src – Source file path
dst – Destination file path
- exists(path: str | Path) bool[source]
Check if path exists.
- Parameters:
path – Path to check
- Returns:
True if path exists, False otherwise
- get_size(path: str | Path) int[source]
Get file size in bytes.
- Parameters:
path – File path
- Returns:
File size in bytes
- glob(pattern: str | Path) Iterator[Path][source]
Glob pattern matching.
- Parameters:
pattern – Glob pattern to match
- Yields:
Matching paths
- is_dir(path: str | Path) bool[source]
Check if path is a directory.
- Parameters:
path – Path to check
- Returns:
True if path is a directory, False otherwise
- is_file(path: str | Path) bool[source]
Check if path is a file.
- Parameters:
path – Path to check
- Returns:
True if path is a file, False otherwise
- listdir(path: str | Path) Iterator[str][source]
List directory contents.
- Parameters:
path – Directory path to list
- Yields:
Directory entry names
- mkdir(path: str | Path, parents: bool = False, exist_ok: bool = False) None[source]
Create a directory.
- Parameters:
path – Directory path to create
parents – Create parent directories if needed
exist_ok – Don’t raise error if directory already exists
- move(src: str | Path, dst: str | Path) None[source]
Move/rename a file or directory.
- Parameters:
src – Source path
dst – Destination path
- open(file: str | Path, mode: str = 'r', encoding: str | None = None) TextIO | BinaryIO[source]
Open a file using built-in open().
- Parameters:
file – File path to open
mode – File mode (r, w, a, rb, wb, etc.)
encoding – Text encoding (for text modes, ignored for binary modes)
- Returns:
File handle (TextIO for text modes, BinaryIO for binary modes)
- read_bytes(path: str | Path) bytes[source]
Read bytes from a file.
- Parameters:
path – File path to read
- Returns:
File contents as bytes
- read_text(path: str | Path, encoding: str = 'utf-8') str[source]
Read text from a file.
- Parameters:
path – File path to read
encoding – Text encoding (default: utf-8)
- Returns:
File contents as string
- rmdir(path: str | Path) None[source]
Remove a directory.
- Parameters:
path – Directory path to remove (must be empty)
- unlink(path: str | Path, missing_ok: bool = False) None[source]
Remove a file.
- Parameters:
path – File path to remove
missing_ok – Don’t raise error if file doesn’t exist
- class pyiv.files.TarFilesystem(source: str | Path | bytes)[source]
Bases:
FilesystemTreat a tar archive as a read-oriented
Filesystemtree.Why this exists: Same consumer API as disk/memory/zip for bundled trees.
Example
>>> import io, tarfile >>> from pyiv.files import TarFilesystem >>> buf = io.BytesIO() >>> with tarfile.open(fileobj=buf, mode="w") as tf: ... info = tarfile.TarInfo(name="a.txt") ... data = b"x" ... info.size = len(data) ... tf.addfile(info, io.BytesIO(data)) >>> fs = TarFilesystem(buf.getvalue()) >>> fs.read_text("/a.txt") 'x'
- copy(src: str | Path, dst: str | Path) None[source]
Copy a file.
- Parameters:
src – Source path
dst – Destination path
- exists(path: str | Path) bool[source]
Check if a path exists.
- Parameters:
path – Path to check
- Returns:
True if path exists, False otherwise
- get_size(path: str | Path) int[source]
Get file size in bytes.
- Parameters:
path – File path
- Returns:
File size in bytes
- glob(pattern: str | Path) Iterator[Path][source]
Glob pattern matching.
- Parameters:
pattern – Glob pattern
- Yields:
Matching paths
- is_dir(path: str | Path) bool[source]
Check if path is a directory.
- Parameters:
path – Path to check
- Returns:
True if path is a directory, False otherwise
- is_file(path: str | Path) bool[source]
Check if path is a file.
- Parameters:
path – Path to check
- Returns:
True if path is a file, False otherwise
- listdir(path: str | Path) Iterator[str][source]
List directory contents.
- Parameters:
path – Directory path
- Yields:
Directory entry names
- mkdir(path: str | Path, parents: bool = False, exist_ok: bool = False) None[source]
Create a directory.
- Parameters:
path – Directory path
parents – Create parent directories if needed
exist_ok – Don’t raise error if directory exists
- move(src: str | Path, dst: str | Path) None[source]
Move/rename a file or directory.
- Parameters:
src – Source path
dst – Destination path
- open(file: str | Path, mode: str = 'r', encoding: str | None = None) TextIO | BinaryIO[source]
Open a file.
- Parameters:
file – File path
mode – File mode (r, w, a, rb, wb, etc.)
encoding – Text encoding (for text modes)
- Returns:
File handle
- read_bytes(path: str | Path) bytes[source]
Read bytes from a file.
- Parameters:
path – File path
- Returns:
File contents as bytes
- read_text(path: str | Path, encoding: str = 'utf-8') str[source]
Read text from a file.
- Parameters:
path – File path
encoding – Text encoding
- Returns:
File contents as string
- unlink(path: str | Path, missing_ok: bool = False) None[source]
Remove a file.
- Parameters:
path – File path
missing_ok – Don’t raise error if file doesn’t exist
- class pyiv.files.ZipFilesystem(source: str | Path | bytes, *, mode: Literal['r', 'w', 'x', 'a'] = 'r')[source]
Bases:
FilesystemTreat a ZIP archive as a
Filesystemtree (read; write when writable).Why this exists: Apps that read config or assets from a zip should inject the same
FilesystemAPI as disk/memory, including underMountFilesystem.Example
>>> import io, zipfile >>> from pyiv.files import ZipFilesystem >>> buf = io.BytesIO() >>> with zipfile.ZipFile(buf, "w") as zf: ... zf.writestr("hello.txt", "hi") >>> fs = ZipFilesystem(buf.getvalue()) >>> fs.read_text("/hello.txt") 'hi'
- __init__(source: str | Path | bytes, *, mode: Literal['r', 'w', 'x', 'a'] = 'r') None[source]
Open a zip from a path or in-memory bytes.
- Parameters:
source – Path to a
.zipfile, or raw zip bytesmode –
r(read-only) ora/wwhensourceis a path
- copy(src: str | Path, dst: str | Path) None[source]
Copy a file.
- Parameters:
src – Source path
dst – Destination path
- exists(path: str | Path) bool[source]
Check if a path exists.
- Parameters:
path – Path to check
- Returns:
True if path exists, False otherwise
- get_size(path: str | Path) int[source]
Get file size in bytes.
- Parameters:
path – File path
- Returns:
File size in bytes
- glob(pattern: str | Path) Iterator[Path][source]
Glob pattern matching.
- Parameters:
pattern – Glob pattern
- Yields:
Matching paths
- is_dir(path: str | Path) bool[source]
Check if path is a directory.
- Parameters:
path – Path to check
- Returns:
True if path is a directory, False otherwise
- is_file(path: str | Path) bool[source]
Check if path is a file.
- Parameters:
path – Path to check
- Returns:
True if path is a file, False otherwise
- listdir(path: str | Path) Iterator[str][source]
List directory contents.
- Parameters:
path – Directory path
- Yields:
Directory entry names
- mkdir(path: str | Path, parents: bool = False, exist_ok: bool = False) None[source]
Create a directory.
- Parameters:
path – Directory path
parents – Create parent directories if needed
exist_ok – Don’t raise error if directory exists
- move(src: str | Path, dst: str | Path) None[source]
Move/rename a file or directory.
- Parameters:
src – Source path
dst – Destination path
- open(file: str | Path, mode: str = 'r', encoding: str | None = None) TextIO | BinaryIO[source]
Open a file.
- Parameters:
file – File path
mode – File mode (r, w, a, rb, wb, etc.)
encoding – Text encoding (for text modes)
- Returns:
File handle
- read_bytes(path: str | Path) bytes[source]
Read bytes from a file.
- Parameters:
path – File path
- Returns:
File contents as bytes
- read_text(path: str | Path, encoding: str = 'utf-8') str[source]
Read text from a file.
- Parameters:
path – File path
encoding – Text encoding
- Returns:
File contents as string
- unlink(path: str | Path, missing_ok: bool = False) None[source]
Remove a file.
- Parameters:
path – File path
missing_ok – Don’t raise error if file doesn’t exist