Skip to main content

Small utilities for listing files in directories

Project description

fileutils-dir: Fluent Python API for Declarative File Discovery and Filtering

fileutils-dir is a lightweight, zero-dependency Python library designed for efficient filesystem traversal and file selection. It provides a chainable interface to filter files by extension, name patterns, semantic categories, and directory depth without modifying the underlying filesystem.

This library is specifically engineered as a feeder tool for data pipelines, automation scripts, and application logic that requires precise control over batch file processing.

Installation

Install the package via pip:

pip install fileutils-dir

Design Philosophy

The library follows a declarative approach to file selection:

  1. Single Entry Point: All operations begin with the in_dir() function.
  2. Fluent Interface: Methods are chainable, allowing complex queries to be built incrementally.
  3. Lazy Evaluation: Selection logic is stored and only executed when a terminal method (list() or count()) is invoked.
  4. Non-Destructive: The library strictly performs read-only operations on the filesystem structure. It does not create, delete, or modify files.

Selection Logic and Composability

Selectors are monotonic and composable. Filters applied later in the chain refine the candidate set established by previous methods.

  • Inclusive Filters (include_ext, include_type): Restrict the result set to items matching the specified criteria.
  • Exclusive Filters (exclude_ext, exclude_type): Remove items matching the specified criteria from the result set.
  • Precedence: If a file matches both an inclusive and an exclusive filter, the exclusion rule takes precedence.

API Documentation

Initializer

in_dir(*paths)

Initializes a DirQuery object. If no paths are provided, it defaults to the current working directory ("."). Accepts multiple path arguments to query across several root directories simultaneously. Results are deduplicated based on resolved physical paths.

Selection Methods

.name(pattern: str)

Filters results using a glob-style name pattern (e.g., "*.py", "test_*").

.include_ext(*exts: str)

Specifies file extensions to include in the result set. Extensions are case-insensitive and can be provided with or without the leading dot.

.exclude_ext(*exts: str)

Specifies file extensions to exclude from the result set.

.include_type(*types: str)

Narrows the selection based on semantic categories (e.g., "image", "code", "data"). See Semantic Classification for details.

.exclude_type(*types: str)

Removes specific semantic categories from the selection.

Traversal and Mode Methods

.recursive(recursive_depth: int = None)

Enables recursive traversal through subdirectories.

  • If recursive_depth is None (default), traversal is bottomless.
  • If recursive_depth is an integer, it restricts traversal to the specified depth (0 represents the root directory level, 1 includes immediate subdirectories, etc.).

.dirs()

Configures the query to return directory paths instead of file paths.

.show_hidden()

Includes hidden files and directories (those starting with a dot) in the results.

Terminal Methods

.list() -> list[str]

Executes the defined query and returns a list of absolute or relative file paths as strings.

.count() -> int

Executes the defined query and returns the total number of matched items.

Introspection

repr(query)

Printing or inspecting the DirQuery object returns a summary of the current query configuration.

Semantic Classification

The library supports high-level semantic filtering, mapping common file categories to their respective extensions:

Category Associated Extensions
image .jpg, .jpeg, .png, .webp, .bmp, .gif, .tiff
text .txt, .md, .rst, .log
pdf .pdf
doc .doc, .docx, .odt
sheet .xls, .xlsx, .ods, .csv
presentation .ppt, .pptx, .odp
code .py, .js, .ts, .java, .c, .cpp, .h, .go, .rs, .rb, .php, .sh
data .json, .yaml, .yml, .xml, .toml
audio .mp3, .wav, .flac, .ogg, .aac, .m4a
video .mp4, .mkv, .avi, .mov, .webm
archive .zip, .tar, .gz, .bz2, .7z, .rar

Examples

Discovering Python Source Files with Depth Limit

from fileutils import in_dir

# Search 'src' and its immediate children for .py files
source_files = (
    in_dir("src")
    .recursive(recursive_depth=1)
    .include_ext("py")
    .list()
)

Multi-Root Image Selection

from fileutils import in_dir

# Query multiple directories for images, excluding PNGs
images = (
    in_dir("assets/icons", "assets/banners")
    .include_type("image")
    .exclude_ext("png")
    .list()
)

Technical Specifications

  • Python Version: Requires Python 3.9 or higher.
  • Operating System: Platform-independent (Windows, macOS, Linux).
  • Dependencies: Standard library only (pathlib).
  • Version: 0.10.0
  • License: MIT.

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

fileutils_dir-0.10.0.tar.gz (5.0 kB view details)

Uploaded Source

Built Distribution

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

fileutils_dir-0.10.0-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

Details for the file fileutils_dir-0.10.0.tar.gz.

File metadata

  • Download URL: fileutils_dir-0.10.0.tar.gz
  • Upload date:
  • Size: 5.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.10

File hashes

Hashes for fileutils_dir-0.10.0.tar.gz
Algorithm Hash digest
SHA256 3fb6253c954bb6b665f4f0b949b71e442c77b7c8538bcfe0f83dcd1b8122474e
MD5 b4d1ca170dbc5bc4cd29ff55e4649a61
BLAKE2b-256 aa9da8716408bf766669a9989a9c5295e77190f6cc660d9b3e152f1d3ec0ca78

See more details on using hashes here.

File details

Details for the file fileutils_dir-0.10.0-py3-none-any.whl.

File metadata

  • Download URL: fileutils_dir-0.10.0-py3-none-any.whl
  • Upload date:
  • Size: 5.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.10

File hashes

Hashes for fileutils_dir-0.10.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7ee76ec586039552a2cdbdb1dfa14db23df03232270e7ae084fc9b81628c27b0
MD5 2ca82a301e8ac45c2dcf6f8660a2bc6b
BLAKE2b-256 ee405c795937cd36c8e39d85762b9e9b9c8745dc561054b287cc2d3505354ed7

See more details on using hashes here.

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