pygount

3.2.0 · active · verified Tue Apr 14

Pygount is a command line tool designed to scan folders for source code files and accurately count the number of source lines of code (SLOC). It leverages the robust `pygments` package to parse source code, allowing it to analyze hundreds of programming languages. Pygount, currently at version 3.2.0, is an actively maintained library with regular updates.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `pygount`'s API to programmatically analyze source code files within a directory and summarize the results. It creates dummy files, analyzes them using `SourceAnalysis` and aggregates the results with `ProjectSummary`, then prints the language-specific counts.

import os
from pathlib import Path
from pygount import SourceAnalysis, ProjectSummary

# Create dummy files for demonstration
dummy_dir = Path('./my_project')
dummy_dir.mkdir(exist_ok=True)
(dummy_dir / 'main.py').write_text(
    """# This is a Python file
import sys

def hello(): # Code line
    print('Hello, world!') # Code line
    # Another comment
"""
)
(dummy_dir / 'README.md').write_text(
    """# My Project

This is a test project.
"""
)
(dummy_dir / '.hidden_file.txt').write_text("should not be counted")

project_summary = ProjectSummary()

# Analyze files in the dummy project directory
for source_path in dummy_dir.rglob('*'):
    if source_path.is_file():
        try:
            source_analysis = SourceAnalysis.from_file(source_path, 'my_project')
            project_summary.add(source_analysis)
        except UnicodeDecodeError as e:
            print(f"Warning: Could not decode {source_path}: {e}")

print("\n--- Analysis Results ---")
for language_summary in project_summary.language_to_language_summary_map.values():
    print(language_summary)

# Clean up dummy files
import shutil
shutil.rmtree(dummy_dir)

view raw JSON →