pathScripts/linecount

70 lines
2.2 KiB
Python
Executable file

#!/usr/bin/env python3
import os
import sys
def is_binary(file_path):
"""
Determines if a file is binary by checking its content.
Returns True for binary files, False for text files.
"""
try:
with open(file_path, 'rb') as f:
# Read the first 1024 bytes to check if it's binary
chunk = f.read(1024)
if b'\0' in chunk:
return True
return False
except Exception as e:
print(f"Error reading file {file_path}: {e}")
return True # Treat unreadable files as binary for safety.
def count_lines_in_file(file_path):
"""
Counts the number of lines in a given text file.
"""
try:
with open(file_path, 'r', encoding='utf-8', errors='ignore') as f:
return sum(1 for _ in f)
except Exception as e:
print(f"Error counting lines in file {file_path}: {e}")
return 0
def count_lines_in_directory(directory, extensions=None):
"""
Recursively counts lines in all text files (optionally filtered by extensions) within the directory.
"""
total_lines = 0
total_files = 0
for root, _, files in os.walk(directory):
for file_name in files:
file_path = os.path.join(root, file_name)
# Skip binary files
if is_binary(file_path):
continue
# Check for extensions if provided
if extensions and not file_name.lower().endswith(tuple(extensions)):
continue
# Count lines in the valid file
lines = count_lines_in_file(file_path)
total_lines += lines
total_files += 1
return total_files, total_lines
if __name__ == "__main__":
# Get extensions from command-line arguments
extensions = [ext.lower() for ext in sys.argv[1:]] if len(sys.argv) > 1 else None
# Get the current working directory
current_dir = os.getcwd()
print(f"Scanning directory: {current_dir}")
if extensions:
print(f"Filtering by extensions: {', '.join(extensions)}")
total_files, total_lines = count_lines_in_directory(current_dir, extensions)
print(f"Total matching files: {total_files}")
print(f"Total lines across matching files: {total_lines}")