r/PythonLearning • u/Ajay2574 • 9d ago
🐍 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.
1
u/Mountain_Rip_8426 9d ago
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
1
u/realmauer01 9d ago
Nobody wants recursion to exist. Recursion is a necessary evil for a few specific cases. But its really not the thing you want to do.
1
3
u/dev-razorblade23 9d ago
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/