Skip to main content

file-primitives

  • read_file: reads file with an option to specify default if file does not exist or a custom reader (like reader = lambda file: json.read(file))
  • write_file: writes file with auto-directory creation, custom writer, and optional atomic writes
  • ensure_dir: ensures a directory exists
  • delete_path: deletes a file or directory. Returns bool indicating success.
def read_file(
    path: Union[str, Path],
    bytes: bool = False,
    reader: Callable[..., T] = lambda file: file.read(),
    default: Union[D, _MissingType] = MISSING,
    encoding: str = "utf-8",
) -> Union[T, D]:
    pass

def write_file(
    path: Union[str, Path],
    data: T,
    bytes: bool = False,
    writer: Callable[[T, Any], Any] = lambda data, file: file.write(data),
    encoding: str = "utf-8",
    ensure_dir: bool = True,
    atomic: bool = False,
) -> None:
    pass

def ensure_dir(
    path: T,  # T = TypeVar("T", str, Path)
    is_file: bool = False,
) -> T:
    pass

def delete_path(
    path: Union[str, Path],
    missing_ok: bool = False,
    delete_empty_parents: bool = False,
) -> bool:
    pass

...

from file_primitives import read_file, write_file, ensure_dir, delete_path

# Simple file operations
content = read_file("config.json")

# Binary mode
binary_content = read_file("image.png", bytes=True)

# output dir will be created automatically by default
write_file("output/data.json", {"key": "value"}, writer=lambda data, file: json.dump(data, file))

# Atomic write
write_file("important.json", data, atomic=True)

# With error handling
config = read_file("missing.json", default="I am missing")

ensure_dir("logs/2024/january")  # Creates full path
ensure_dir("reports/summary.pdf", is_file=True)  # Creates "reports/" directory

# Delete operations
delete_path("temp/file.txt")
deleted = delete_path("temp/file.txt", missing_ok=True)

Metadata

Release files for file-primitives 0.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for file-primitives 0.2.1
File Size Uploaded
file_primitives-0.2.1.tar.gz 41.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for file-primitives 0.2.1
File Interpreter ABI Platform
file_primitives-0.2.1-py3-none-any.whl Python 3 none any Details

Total release size: 48.0 kB

Release files / file_primitives-0.2.1.tar.gz

Download URL file_primitives-0.2.1.tar.gz
Size 41.8 kB
Tags Source
SHA-256 checksum
How to use checksums
8f70e58dfbbbe2618d4645f1aaf6d5e7d49f68136d86486312b0a88275bbcde6
BLAKE2b-256 checksum
How to use checksums
250cb3cfd5cc741811bf9a22a5687cc0e1d0bf8eaff1f6d1ff378bd7d68dfb68
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.7.2

Release files / file_primitives-0.2.1-py3-none-any.whl

Download URL file_primitives-0.2.1-py3-none-any.whl
Size 6.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2c8e52eac6d1c5d81c620f733582ac4ddc344230d35aef9eba590c5c1cf4544e
BLAKE2b-256 checksum
How to use checksums
089a287e10aa0f5443e5ce58a6bd67b90756aa2db36181a581299379e2446021
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.7.2

Release history Release notifications | RSS feed

This release

0.2.1 This release

2 release files

0.2.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page