Katkat

Search a Directory Tree for a File and Return Its Absolute Path in Python

CrestaSoftware Engineer · Online Assessment

Last updated

Write a Python function that searches a directory and all of its subdirectories for a file matching a requirement and returns the file's absolute path. It tests practical use of os and pathlib, deterministic handling of multiple or missing matches, relative roots, symbolic links and unreadable directories.

You are given the path of a directory. Write a Python function that searches that directory and all of its subdirectories for the file that meets a given requirement, and returns that file's absolute path.

This is a practical programming task rather than an algorithm puzzle: you are expected to use the standard library's filesystem tools, such as os and pathlib, fluently. 30 minutes are allotted to it.

A possible interface, in which the file is identified by its name (adapt it to the requirement you are given):

def find_file(root: str, target_name: str) -> str | None:
 """Return the absolute path of the file named target_name under root, or None."""

Constraints and Clarifications

  • The search covers the given directory and every directory below it.
  • The returned path must be absolute, even when root is passed as a relative path such as ".".
  • Not specified: the exact requirement that identifies the file. The interface above assumes an exact file name; be ready to swap in another criterion (an extension, a pattern, a size, the file's contents) without rewriting the traversal.

Clarifying Questions

  • What identifies the file: an exact name, an extension or pattern, or something inside it?
  • If several files match, should the function return the first one found, the one closest to the root, or all of them, and in what order?
  • What should happen when nothing matches: return None, raise an error, or return an empty list?
  • Should the search follow symbolic links to directories, and should the returned path have its symbolic links resolved?
  • How should directories you are not allowed to read be handled, and should hidden directories be searched?

Hints

Hint 1

Let the library walk: The file can be at any depth. Decide which standard-library call visits every subdirectory for you, and what it hands you at each level.

Hint 2

Joined is not absolute: Joining a relative root with a file name still gives a relative path. Decide where the path becomes absolute, and whether symbolic links should be resolved on the way.

Follow-up questions

  • Return every match instead of one, sorted so that the output is identical on every operating system.
  • The tree holds millions of files on a network drive. What makes the search slow, and how do you speed it up?
  • Find the file by a string it contains rather than by its name. What changes, and what do you guard against when opening files?
  • How would you unit-test this function without depending on the filesystem layout of the machine it runs on?