[Rod Stephens Books]
Index Books Python Examples About Rod Contact
[Mastodon] [Bluesky] [Facebook]
[Build Your Own Python Action Arcade!]

[Build Your Own Ray Tracer With Python]

[Beginning Database Design Solutions, Second Edition]

[Beginning Software Engineering, Second Edition]

[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

Title: Count the number of lines in the files in a directory in Python

[The number of lines in the files in one directory]

I recently needed to know how many lines of code were in a specific project, so I wrote this super simple Python program.

Counting Lines in One File

The following function counts the lines in a single file.

def count_lines_in_file(filename): '''Return the number of lines in this file.''' with open(filename, 'r') as file: return sum(1 for line in file)

The phrase for line in file creates an iterable file object that acts like a generator. The code passes it to the sum function to make that function add the value 1 for each line in the file. The result is the number of lines in the file.

Counting Lines in Multiple Files

The following function counts the lines in files inside a directory.

from pathlib import Path def count_lines_in_dir(dirpath, patterns): '''Return the number of files and number of lines in this directory.''' num_files = 0 num_lines = 0 for pattern in patterns: for file in Path(dirpath).glob(pattern): num_files += 1 num_lines += count_lines_in_file(file) print(file, count_lines_in_file(file)) return num_files, num_lines

The patterns parameter is a list holding patterns that the file names should match. For example, the example program uses the list ['*.py', '*.json'] to count the lines in Python files and JSON files.

For each pattern, the function uses Path(dirpath).glob(pattern) to iterate through the files in the directory that match the current pattern.

<ASIDE>The impressively stupid name glob is short for "global." Way back when computers were treadle-powered and made out of coconuts and bamboo (the early 1970s), the Unix command line did not automatically expand wildcards in file names. The glob function was used to "globalize" or expand file names.</ASIDE>

For each file returned by glob, the code increments the file count. It also calls count_lines_in_file to see how many lines the file contains and adds that number to the num_lines count.

The function also prints each file's name and line count. If you don't want that, ignore it, remove that code, or use a parameter to tell the function whether it should display that information.

After it examines all of the files, the function returns the number of files and the total number of lines.

Conclusion

Here's how the example program uses these functions.

dirpath = r'C:\Users\rod\Desktop\Frankies Body Shop' patterns = ['*.py', '*.json'] num_files, num_lines = count_lines_in_dir(dirpath, patterns) print(f'# Files: {num_files}') print(f'# Lines: {num_lines}')

This code simply calls count_lines_in_dir and displays the results.

Download the example to experiment with it and to see additional details.

© 2025 - 2026 Rocky Mountain Computer Consulting, Inc. All rights reserved.