Count Lines of Python Code

I have created a new application called lopc (Lines of Python Code). It is available for install with pip on PyPI (, and the source code is available on GitHub:

The app really only counts the number of non-empty and non-comment lines of *.py files in the given directories. These are some features of the code:

  • The command arguments are parsed with argparse, demonstrating using same option multiple times: -e <dir> (--exclude-dir) can be used for excluding several directories, and -v (--verbose) can be used to increase verbosity (-vv)
  • pathlib is used for path manipulation and file reading
  • Recursive function calls are used for scanning the full directory tree.

Example run:

markku@devel:~$ lopc devel/lopc -v -e build
File: devel/lopc/ (16 lines)
File: devel/lopc/lopc/ (5 lines)
File: devel/lopc/lopc/ (124 lines)
File: devel/lopc/lopc/ (0 lines)
devel/lopc Files: 4 Lines: 145
Updated: November 27, 2022 — 21:02

Leave a Reply