2026-04-10 18:10 UTC
Python Tip #100 (of 365):
Prefer the pathlib.Path object's is_file() and is_dir() methods over exists()
The exists method tells you SOMETHING is there but not WHAT.
Using is_file() or is_dir() is more explicit about what you're actually checking for, and usually what you actually want.
Usually when I find myself using exists(), I'll realize that I wanted one of the more specific methods instead.
#Python #DailyPythonTip
Replies (1)
-
@henryiii@fosstodon.org 2026-04-10 18:14
@treyhunner Also the Traversable Protocol only has is_file and is_dir, not exists. :) https://docs.python.org/3/library/importlib.resources.abc.html#importlib.resources.abc.Traversable