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 / MemoryFilesystem

  • MountFilesystem / MountPoint

  • ZipFilesystem / TarFilesystem

  • MappingFilesystem (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

Filesystem()

Abstract filesystem for injectable file I/O.

RealFilesystem([root])

Real filesystem implementation using the standard library.

MemoryFilesystem()

In-memory filesystem for testing.

MountFilesystem([root])

Path-prefix router over nested Filesystem instances.

MountPoint(mount_path, filesystem)

A path prefix bound to a nested filesystem.

ZipFilesystem(source, *[, mode])

Treat a ZIP archive as a Filesystem tree (read; write when writable).

TarFilesystem(source)

Treat a tar archive as a read-oriented Filesystem tree.

MappingFilesystem([data])

Expose a string→bytes (or str) mapping as a flat file tree under /.

class pyiv.files.Filesystem[source]

Bases: ABC

Abstract filesystem for injectable file I/O.

Why this exists: Production code that calls open() / pathlib directly is hard to unit-test without touching disk or mocking builtins. Depend on Filesystem and bind RealFilesystem in production and MemoryFilesystem in 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

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.

abstract write_bytes(path: str | Path, content: bytes) → None[source]

Write bytes to a file.

Parameters:
  • path – File path

  • content – Bytes content to write

abstract write_text(path: str | Path, content: str, encoding: str = 'utf-8') → None[source]

Write text to a file.

Parameters:
  • path – File path

  • content – Text content to write

  • encoding – Text encoding

class pyiv.files.MappingFilesystem(data: Mapping[str, str | bytes] | None = None)[source]

Bases: Filesystem

Expose a string→bytes (or str) mapping as a flat file tree under /.

Why this exists: Pattern for external adapters (DB row as /proc file) without putting SQL in core. Mount under MountFilesystem.

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'
__init__(data: Mapping[str, str | bytes] | 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

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

rmdir(path: str | Path) → None[source]

Remove a directory.

Parameters:

path – Directory path

Remove a file.

Parameters:
  • path – File path

  • missing_ok – Don’t raise error if file doesn’t exist

write_bytes(path: str | Path, content: bytes) → None[source]

Write bytes to a file.

Parameters:
  • path – File path

  • content – Bytes content to write

write_text(path: str | Path, content: str, encoding: str = 'utf-8') → None[source]

Write text to a file.

Parameters:
  • path – File path

  • content – Text content to write

  • encoding – Text encoding

class pyiv.files.MemoryFilesystem[source]

Bases: Filesystem

In-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'
__init__()[source]

Initialize in-memory filesystem.

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:
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:
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:

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

write_bytes(path: str | Path, content: bytes) → None[source]

Write bytes to a file.

Parameters:
  • path – File path to write

  • content – Bytes content to write

write_text(path: str | Path, content: str, encoding: str = 'utf-8') → None[source]

Write text to a file.

Parameters:
  • path – File path to write

  • content – Text content to write

  • encoding – Text encoding (default: utf-8)

class pyiv.files.MountFilesystem(root: Filesystem | None = None)[source]

Bases: Filesystem

Path-prefix router over nested Filesystem instances.

Why this exists: Expose virtual trees (fake /proc, zip mounts, etc.) beside a real or memory root without monkeypatching open.

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 filesystem at mount_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

rmdir(path: str | Path) → None[source]

Remove a directory.

Parameters:

path – Directory path

Remove a file.

Parameters:
  • path – File path

  • missing_ok – Don’t raise error if file doesn’t exist

unmount(mount_path: str | Path) → None[source]

Remove a mount by path prefix.

write_bytes(path: str | Path, content: bytes) → None[source]

Write bytes to a file.

Parameters:
  • path – File path

  • content – Bytes content to write

write_text(path: str | Path, content: str, encoding: str = 'utf-8') → None[source]

Write text to a file.

Parameters:
  • path – File path

  • content – Text content to write

  • encoding – Text encoding

class pyiv.files.MountPoint(mount_path: str, filesystem: Filesystem)[source]

Bases: object

A 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
mount_path: str
class pyiv.files.RealFilesystem(root: str | Path | None = None)[source]

Bases: Filesystem

Real filesystem implementation using the standard library.

Use this in production when you need actual disk I/O. Prefer MemoryFilesystem in unit tests so examples and CI never write into the working tree. Optional root= 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'
__init__(root: str | Path | None = None) → None[source]
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)

Remove a file.

Parameters:
  • path – File path to remove

  • missing_ok – Don’t raise error if file doesn’t exist

write_bytes(path: str | Path, content: bytes) → None[source]

Write bytes to a file.

Parameters:
  • path – File path to write

  • content – Bytes content to write

write_text(path: str | Path, content: str, encoding: str = 'utf-8') → None[source]

Write text to a file.

Parameters:
  • path – File path to write

  • content – Text content to write

  • encoding – Text encoding (default: utf-8)

class pyiv.files.TarFilesystem(source: str | Path | bytes)[source]

Bases: Filesystem

Treat a tar archive as a read-oriented Filesystem tree.

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'
__init__(source: str | Path | bytes) → 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

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

rmdir(path: str | Path) → None[source]

Remove a directory.

Parameters:

path – Directory path

Remove a file.

Parameters:
  • path – File path

  • missing_ok – Don’t raise error if file doesn’t exist

write_bytes(path: str | Path, content: bytes) → None[source]

Write bytes to a file.

Parameters:
  • path – File path

  • content – Bytes content to write

write_text(path: str | Path, content: str, encoding: str = 'utf-8') → None[source]

Write text to a file.

Parameters:
  • path – File path

  • content – Text content to write

  • encoding – Text encoding

class pyiv.files.ZipFilesystem(source: str | Path | bytes, *, mode: Literal['r', 'w', 'x', 'a'] = 'r')[source]

Bases: Filesystem

Treat a ZIP archive as a Filesystem tree (read; write when writable).

Why this exists: Apps that read config or assets from a zip should inject the same Filesystem API as disk/memory, including under MountFilesystem.

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 .zip file, or raw zip bytes

  • mode – r (read-only) or a / w when source is 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

rmdir(path: str | Path) → None[source]

Remove a directory.

Parameters:

path – Directory path

Remove a file.

Parameters:
  • path – File path

  • missing_ok – Don’t raise error if file doesn’t exist

write_bytes(path: str | Path, content: bytes) → None[source]

Write bytes to a file.

Parameters:
  • path – File path

  • content – Bytes content to write

write_text(path: str | Path, content: str, encoding: str = 'utf-8') → None[source]

Write text to a file.

Parameters:
  • path – File path

  • content – Text content to write

  • encoding – Text encoding