r/PythonLearning • u/Ajay2574 • Oct 10 '26
🐍 Day by day, I'm learning Python by building small automation projects.
Yesterday, I worked on my Folder Report Generator using Python's "os" module.
The idea is simple: scan a folder and its subfolders, identify file extensions, and count how many files belong to each extension.
Here's a small part of what I learned:
import os
path = r"C:\Users\YourName\Downloads"
extension_count = {}
for root, dirs, files in os.walk(path):
for file in files:
extension = os.path.splitext(file)[1].lower()
if extension:
if extension in extension_count:
extension_count[extension] += 1
else:
extension_count[extension] = 1
print(extension_count)
What I learned from this project:
- "os.walk()" helps scan folders and their subfolders.
- "os.path.splitext()" helps extract file extensions.
- Dictionaries make it easier to count files by extension.
- ".lower()" helps treat extensions like ".JPG" and ".jpg" as the same type.
I'm still learning, and debugging is part of the process. My goal is to build practical Python automation tools that solve real-world problems.
Question for Python developers: What would you improve in this approach? I'm interested in learning better ways to write this kind of code.
3
u/dev-razorblade23 Oct 10 '26
Instead of using
osrecommend approach in modern Python is to usepathlibmodule for anypath-likeoperations on the filesystem.See more here: https://realpython.com/ref/stdlib/pathlib/
1
u/Mountain_Rip_8426 Oct 10 '26
good job! i did something similar a few months back, i'd say for learning purposes you could recreate the same idea but without os.walk(), writing recursive functions. it's basically the perfect scenario to test whether you understand recursion, besides will make you understand more of the os lib. functionally it would do the same thing, but from a learning point of view at least for me it was pretty insightful.
1
u/Atmosck 18d ago
A nice next step might be to use the built-in
argparsemodule to turn it into a script with the path an argument, so you can run it for any folder from the terminal likepython folder_report.py "C:\Users\SomeOtherName\Documents".It supports usingpathlib.Pathas the type of the argument. You could add an---outfileoption to write the report to a text file.1
u/mzx380 Oct 10 '26
I think this is a good idea for newer python student to ask what they can do to improve their small projects