Projects


A short python script I made which counts all the lines of code in a given directory.

useState

Code counter


Code counter


How does it work?
It first takes in the name of a directory and iterates through every item inside the directory. If the item is a source file, it will open it and count the number of lines in the file. Let's name this process step 1. If the item is a folder, it will recursively repeat step 1.

What about media files, images, videos, non-ASCII files?
Attempting to open a non-ASCII file in read mode will raise a UnicodeDecodeError and the file is ignored.


Although simple in concept, this is one of the most useful script I have made since I find myself using this script almost every time I have to analyze a relatively big project I work in order to optimize it.


References
ignore(_file_or_folder_) - Ignores a file or folder in every directory.
ignore_absolute_path(_path_) - Ignores an absolute path.


How to use it?
Clone the github repository.
app.py

from countLine import Source as Counter  
counter = Counter("src") # Source directory  
counter.ignore("__init__.py") # Ignore a file in every directory  
counter.ignore("__pycache__") # Ignore a folder in every directory  
counter.ignore_absolute_path("pages/static/assets/svg/") # Ignore an absolute path
  
print(f"Total lines of code: {counter.count_lines()}")

And that's it!
Keep track of your project's code with ease.


Mohammad Arafat Zaman © 2024


All rights reserved