Created
February 16, 2024 08:19
-
-
Save jakerieger/5e68578136dc752ba375557c92821f26 to your computer and use it in GitHub Desktop.
Count lines of code in directory
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Counts the total number of lines in all text files of | |
a specified directory, filtered by extension | |
Ex. | |
$ python3 source_line_count.py ~/Code/DataStructures ".c,.h" | |
''' | |
import sys | |
import os | |
search_dir = sys.argv[1] | |
extensions = sys.argv[2].split(",") | |
total_count = 0 | |
largest_count = 0 | |
largest_filename = "" | |
for dirpath, dirs, files in os.walk(search_dir): | |
for filename in files: | |
filepath = os.path.join(dirpath, filename) | |
for ext in extensions: | |
if ext in filename: | |
with open(filepath, "r") as f: | |
count = len(f.readlines()) | |
if count > largest_count: | |
largest_count = count | |
largest_filename = filename | |
total_count += count | |
print("Total Lines : {}".format(total_count)) | |
print("Longest Count : {}".format(largest_count)) | |
print("Longest File : {}".format(largest_filename)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment