Zum Inhalt springen

findpath – give it just the filename and it’ll hand you the absolute path

Hey r/programming 👋

I hacked together a tiny helper called findpath whose entire pitch is: you only need to remember the name of the file. Point it at a logical “root”, tell it the filename, and it’ll walk everything under that root and return the single absolute path that matches—otherwise it raises clear, typed exceptions (file not found / more than one match / root not set, etc.).

Why bother?

  • 🔎 Zero mental overhead – stop remembering long nested directories; remember just settings.yaml.
  • 🧯 Clear failure modes – you’ll know if the file doesn’t exist or exists more than once.
  • 🧰 No third‑party deps – pure stdlib (os).
  • 🧪 Easy to unit test – deterministic, small surface area.
  • 🧱 Works anywhere – POSIX/Windows (relies on os, not shell tricks).

Attributes (all exposed as properties with sanity checks)

  • root: (required) folder name that marks the boundary you’ll search under (e.g., "my_project").
  • root_path: absolute path of that root folder once discovered.
  • all_paths: list of every file path under root_path found during the scan.
  • file: the file name you’re currently searching for.
  • your_path: the final resolved absolute path to the single matching file.
  • current_dir: the working directory when the object is created (captured via os.getcwd()).

Methods

  • find(desired_file: str) -> str Main entrypoint. Walks up from current_dir until it hits root, then recursively scans down. Returns the absolute path if there’s exactly one match, else raises.
  • find_all_paths(root_path: str) -> list[str] Recursively collects all file paths below root_path. Used internally by find, but you can call it yourself if you want the raw list.

Looking for collaborators

I’m still pretty new to Python packaging and open-source contributions, so this project is as much a learning experienceas it is a useful tool. If you’re an experienced Python dev, I’d love your feedback or help making this more “production-ready.”

Things I’d especially appreciate help with:

  • Making the code more Pythonic (e.g., better use of pathlib).
  • Adding tests and CI workflows.
  • Improving error handling with custom exceptions.
  • Documentation improvements (I’m still figuring out best practices).
  • Designing a simple CLI tool.

submitted by /u/karthikeyjoshi
[link] [comments]

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert