Skip to content

Instantly share code, notes, and snippets.

@AJIADb9
Created August 21, 2020 18:15
Show Gist options
  • Select an option

  • Save AJIADb9/fb5271655864d639d69cdd639bdb1720 to your computer and use it in GitHub Desktop.

Select an option

Save AJIADb9/fb5271655864d639d69cdd639bdb1720 to your computer and use it in GitHub Desktop.
Count lines of files in given folder recursively (supports only *.cpp, *.h, *.cs files)
import os
import sys
count = 0
given_path = sys.argv[1]
def count_file(path: str):
global count
file = open(path, 'rb')
file_len = len(file.readlines())
count += file_len
print(f"{file_len} === {path}")
file.close()
def walk_dirs(dir_path: str):
for dirpath, dirnames, filenames in os.walk(dir_path):
for file_name in filenames:
if file_name.endswith(".cpp") or file_name.endswith(".h") or file_name.endswith(".cs"):
file_path = os.path.join(dirpath, file_name)
count_file(file_path)
if os.path.isdir(given_path):
walk_dirs(given_path)
print(count)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment