Skip to main content

Small utilities for listing files in directories

Project description

fileutils

PyPi Version License: MIT Python Versions

A minimalist, fluent Python library for listing and filtering files.

The fileutils library provides a clean, chainable interface to explore directories, filter by extensions, or utilize predefined file categories.


Features

  • Fluent API: Chainable methods designed for high readability and maintainability.
  • Smart Filtering: Filter by extension or semantic categories (e.g., code, data, image).
  • Recursive Support: Deep-search directory structures with a single method call.
  • Directory Trees: Generate nested dictionary representations of folder structures.
  • Directory Filtering: Targeted listing of subdirectories with the same fluent syntax.
  • Hidden Files: Comprehensive control over the visibility of dotfiles.
  • Modern Python: Built-in support for pathlib.Path and comprehensive type hinting.

Installation

pip install fileutils-dir

Usage

The in_dir function returns a query object, providing a descriptive and readable approach to listing or counting files.

1. Basic Listing

from fileutils import in_dir

# List all files in the current directory
files = in_dir().list()

# List all Python files
python_files = in_dir(ext="py").list()

2. Semantic Filtering

Filter files based on their purpose rather than just file extensions.

# List all images and videos
media = in_dir(dtype=["image", "video"]).list()

# Count all data files in a specific folder
count = in_dir(path="data", dtype="data").count()

3. Recursive and Hidden Files

# Find all configuration files recursively, including hidden files
config_files = (
    in_dir(path="project")
    .recursive()
    .show_hidden()
    .list()
)

4. Directory Structures (Tree)

Generate a nested dictionary representing the directory structure.

# Get a tree representation of your project
structure = in_dir("src/").recursive().tree()

5. Listing Directories

Easily switch to listing subdirectories instead of files.

# List all subdirectories
folders = in_dir().dirs().list()

API Reference

in_dir(path=".", *, ext=None, dtype=None)

Initializes a DirQuery object.

  • path: The target directory path (defaults to current directory ".").
  • ext: A string or list of extensions (e.g., "py", [".js", "ts"]).
  • dtype: A string or list of file categories (e.g., "code", "image").

DirQuery Methods

  • .recursive(): Configures the query to traverse all subdirectories.
  • .dirs(): Configures the query to match directories instead of files.
  • .show_hidden(): Includes files starting with a dot (.) in the results.
  • .tree(): Executes the query and returns a nested dictionary representation of the directory.
  • .list(): Executes the query and returns a list[str] of matching paths.
  • .count(): Executes the query and returns the total number of matching items.

Supported Categories (dtype)

Category 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

License

This project is licensed under the MIT License.

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.7.0.tar.gz (4.2 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.7.0-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: fileutils_dir-0.7.0.tar.gz
  • Upload date:
  • Size: 4.2 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.7.0.tar.gz
Algorithm Hash digest
SHA256 ab289e52864cd2f66ae86fd358a73db350b876baca93f36c3bafe80bc908552c
MD5 c3d5587a9ebfe396bfcbb1f943f6d67d
BLAKE2b-256 4402e7d4f52581a2b78bd761ed66b84b035052e6f63a8dd101ac9446975eaff1

See more details on using hashes here.

File details

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

File metadata

  • Download URL: fileutils_dir-0.7.0-py3-none-any.whl
  • Upload date:
  • Size: 4.6 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.7.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1c7b948b8ba790983afd30ba7cc516da4a425e23be103e2451eb1970183f47ea
MD5 98c1b6392bdc0fe3e1ed802582f8d450
BLAKE2b-256 0137862e438621aa160b929879b778124a805f03e6451b8da52485f8e2dae115

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