Stop Using os.path — Learn How Pathlib Simplifies File Handling in Python
Discover how Python’s Pathlib makes file handling effortless. Learn to create, navigate, and manage files with clean, readable code that works across all systems.
Working with files in Python doesn’t have to be awkward, that’s where Pathlib makes life easier.
It’s the modern way to handle files and folders in Python, and once you try it, you’ll never want to go back to using just os or plain string paths. Pathlib treats file paths like real objects instead of basic strings, which makes your code cleaner, safer, and easier to read.
Whether you’re checking if a file exists, creating folders, reading files, or joining paths, Pathlib handles all of it smoothly across different operating systems.
I used to write long, messy path code like os.path.join(”folder”, “subfolder”, “file.txt”) or if not os.path.exists(...), but that’s in the past.
Welcome to Pathlib. Check out other 3 Random Articles here.
Imagine you’re subscribed to a newsletter called 3 Randoms. Each week, it introduces you to three lesser-known Python tools that can make your coding better. It’s like expanding your toolbox and discovering new tricks.
Now, with Pathlib, I can just write Path(”folder”) / “subfolder” / “file.txt”—and it just works. It’s simple, clear, and built right into Python, so there’s nothing extra to install.
Think of Pathlib as your personal file system helper. It knows where your files are, tells you if they exist, creates folders when needed, and lets you open or write to files with very little effort.
In this article, I’ll walk you through a few practical and genuinely useful Pathlib functions that can make your daily coding smoother. From reading and writing files to navigating folders, Pathlib takes what used to be clunky code and turns it into something clean and natural to write.
Let’s jump in, you’ll see how much better file handling in Python can feel once you start working with Paths instead of plain strings.
Thank you guys for allowing me to continue to do work that I find meaningful. This is my full time job so I hope you will support my work. You can now subscribe on the substack app!
My premium readers get access to so much more, like monthly Python projects, in-depth weekly articles, this here ‘3 Randoms’ series, and my complete archive!
👉 If you get value from my work, please leave it a ❤️ and share it. This helps more people discover this newsletter, which helps me out immensely!
This Week’s Pathlib Tips
Keep reading with a 7-day free trial
Subscribe to The Nerd Nook to keep reading this post and get 7 days of free access to the full post archives.



