Created
August 3, 2024 16:24
-
-
Save BennoCrafter/12c99084fdeb16f636284ed2ed9c3aff to your computer and use it in GitHub Desktop.
Get the lines of a project/folder
This file contains 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
import os | |
import argparse | |
ignored_files = [ | |
'.gitignore', # Git ignore file | |
'node_modules', # Node.js dependency folder | |
'npm-debug.log', # NPM debug log | |
'yarn-error.log',# Yarn error log | |
'package-lock.json', # NPM lock file | |
'yarn.lock', # Yarn lock file | |
'.env', # Environment variables | |
'venv', # Python virtual environment | |
'.DS_Store', # macOS Finder metadata | |
'Thumbs.db', # Windows thumbnail cache | |
'*.swp', # Vim swap files | |
'*.swo', # Vim swap files | |
'.vscode', # VSCode settings | |
] | |
ignored_dirs = [ | |
'.idea', # JetBrains IDE settings | |
'__pycache__', # Python bytecode cache | |
'.git', # Git repository | |
'node_modules', # Node.js dependencies | |
'dist', # Distribution folder | |
'build', # Build folder | |
'.vscode', # VSCode settings | |
'.pytest_cache', # Pytest cache | |
'coverage', # Test coverage reports | |
'.cache', # General cache folder | |
'.tox', # Tox environments | |
'env', # Python virtual environment | |
'venv', # Python virtual environment | |
] | |
def count_lines_of_code(directory) -> int: | |
total_lines = 0 | |
for root, dirs, files in os.walk(directory): | |
for file in files: | |
if file in ignored_files: | |
continue | |
if any(dir in root for dir in ignored_dirs): | |
continue | |
if file.endswith('.py'): | |
try: | |
with open(os.path.join(root, file), 'r') as f: | |
try: | |
lines = f.readlines() | |
except UnicodeDecodeError: | |
print(f'Error reading file {file}') | |
continue | |
total_lines += len(lines) | |
except FileNotFoundError: | |
print(f'File {file} not found') | |
continue | |
return total_lines | |
if __name__ == '__main__': | |
parser = argparse.ArgumentParser(description='Count lines of code in a Python project.') | |
parser.add_argument('project_directory', type=str, help='The directory of the project to count lines of code') | |
args = parser.parse_args() | |
total_lines = count_lines_of_code(args.project_directory) | |
print(f'Total lines of code: {total_lines}') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment