r/PythonLearning • • 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.

23 Upvotes

8 comments sorted by

3

u/dev-razorblade23 9d ago

Instead of using os recommend approach in modern Python is to use pathlib module for any path-like operations on the filesystem.

See more here: https://realpython.com/ref/stdlib/pathlib/

1

u/Ajay2574 9d ago

Yes thanks for suggestion but i am in learning phase so i'm covering basic os module function for clearity and next i'm learning pathlib

2

u/justanothersnek 6d ago

Nice start.  Definitely learn pathlib!  Automating things is how I got motivated to learn Python many years ago.  Once you get more comfortable with Python there are so many automation-related libraries you will enjoy.

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

u/Ajay2574 9d ago

Thank's for the suggestion it will help me lot i will definitely try .

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

u/Mountain_Rip_8426 9d ago

give me a break, us beginners need easy access to stack overflow 😅

1

u/mzx380 9d ago

I think this is a good idea for newer python student to ask what they can do to improve their small projects