Trey Hunner 🐍 on Nostr: Python Tip #100 (of 365): Prefer the pathlib.Path object's is_file() and is_dir() ...
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
Published at
2026-04-10 18:10:04 UTCEvent JSON
{
"id": "77d5f218cf045d62a726fd03ce4de8da3878c5642aa46dcb3eb68d38d322b571",
"pubkey": "7e9159fbfd5526415c8d2de2e4c7654e3660883f9ccae720a70fa3c1f3cf1f45",
"created_at": 1775844604,
"kind": 1,
"tags": [
[
"t",
"dailypythontip"
],
[
"t",
"python"
],
[
"proxy",
"https://mastodon.social/@treyhunner/116381751994823856",
"web"
],
[
"proxy",
"https://mastodon.social/users/treyhunner/statuses/116381751994823856",
"activitypub"
],
[
"L",
"pink.momostr"
],
[
"l",
"pink.momostr.activitypub:https://mastodon.social/users/treyhunner/statuses/116381751994823856",
"pink.momostr"
],
[
"-"
]
],
"content": "Python Tip #100 (of 365):\n\nPrefer the pathlib.Path object's is_file() and is_dir() methods over exists()\n\nThe exists method tells you SOMETHING is there but not WHAT.\n\nUsing is_file() or is_dir() is more explicit about what you're actually checking for, and usually what you actually want.\n\nUsually when I find myself using exists(), I'll realize that I wanted one of the more specific methods instead.\n\n#Python #DailyPythonTip",
"sig": "48ed578f762fc7654def6ba274573ad66f412b7ace2c96f43d3021d529f11d4266c52ea372b4a476306686dac1a0caf4b1007164775d7dd89d2233059a6e07bb"
}