Skip to main content

Treat a directory like a dataclass for recursive file listing and reading.

Project description

dirclass

Treat a directory like a dataclass for ease, recursively.

Main features

  • Simple factory: dirclass(path, recursive=True, file_types="*") -> dataclass
  • List files: d.all() returns a flattened list of file paths
  • Read files: d.read_all() returns a flattened list of file contents
  • Filtering: Accept glob patterns like "*.py" or ["*.py", "*.md"] in constructor or per-call
  • Subfolder properties: Immediate subfolders become attributes (e.g., .src, .data)
    • Aliases: full sanitized name, tail after last dot (e.g., path.src.path_src and .src), and case-insensitive access (e.g., .Gallery and .gallery)
  • Robust reading: Read errors are logged and skipped (continues processing)

Install (PyPI)

pip install dirclass

Alternatively,

pip install -e .

Usage

from dirclass import dirclass

# Create a directory wrapper
d = dirclass("/path/to/folder", recursive=True, file_types=["*.py", "*.md"])

# List all matching files
paths = d.all()                 # list[str]

# Read contents of all matching files
contents = d.read_all()         # list[str]

# Override filters per call
py_only = d.all("*.py")
md_and_txt_contents = d.read_all(["*.md", "*.txt"])

# Subfolder properties (immediate children of the root)
# If your root has folders: src, data, and Gallery
src_files = d.src               # files under /path/to/folder/src
data_files = d.data             # files under /path/to/folder/data
gallery_files = d.Gallery       # case-insensitive access also works: d.gallery

# Dotted folder names (e.g., `path.src`) expose multiple aliases
# - full sanitized name: d.path_src
# - tail alias: d.src

Example

from dirclass import dirclass

d = dirclass("./project", recursive=True, file_types=["*.py", "*.md"]) 

print("All files:")
for p in d.all():
    print(p)

print("\nPython files only:")
for p in d.all("*.py"):
    print(p)

print("\nSubfolder: src")
for p in d.src:  # requires a ./project/src subfolder
    print(p)

print("\nReading markdown contents:")
for content in d.read_all("*.md"):
    print(content[:80], "...")

Codes are generated by GPT5, thus may not be reliable in the current version for test.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dirclass-0.1.0.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dirclass-0.1.0-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

Details for the file dirclass-0.1.0.tar.gz.

File metadata

  • Download URL: dirclass-0.1.0.tar.gz
  • Upload date:
  • Size: 5.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for dirclass-0.1.0.tar.gz
Algorithm Hash digest
SHA256 ae6e156dddb38d268cd7808378009513b0a6848594df63990022cd4ed8abcb0e
MD5 0606735c7b76a33a7e1102b9415918e1
BLAKE2b-256 e4f1ad00679b03f4e227a23e74e04dbb078e327c804bd68659081e35fa6dc137

See more details on using hashes here.

Provenance

The following attestation bundles were made for dirclass-0.1.0.tar.gz:

Publisher: python-publish.yml on reycn/dirclass

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file dirclass-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: dirclass-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for dirclass-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 20c4862d1e0e7a7e987f1b4646b11b250222265219cefed03151c67c423d6da8
MD5 2405aceeb8b738c5f2afeb17b71c9421
BLAKE2b-256 2d4f30a411fc46cf6a333953e0700170177890cd5a80f87783e488f64991a00e

See more details on using hashes here.

Provenance

The following attestation bundles were made for dirclass-0.1.0-py3-none-any.whl:

Publisher: python-publish.yml on reycn/dirclass

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page