Line Count
A library that counts the lines within a file, directory, or directory tree.
It can be run through the command line, or functions can be imported to be used in your own Python projects.
Some benefits of linecount include:
- See the number of source lines of code (SLOC), commented lines, and blank lines
- Exclude files or directories whose names contain certain characters. This is useful for excluding files with certain file extensions, or certain directories like
venvor.git. - Only include files whose names contain certain characters. This is useful for counting lines of files with certain file extensions.
You can view the PyPi page here
Installing
You can install the project from pypi:
$ pip install linecount
Command Line Usage
Help
You can get help on commands by performing:
$ linecount -h
$ linecount --help # alternative way to write it
Counting Lines of a File
$ linecount file_to_count.txt
Counting Lines in a Directory
$ linecount . # count all files in this directory
$ linecount /path/to/dir # or specify a directory this way
Recursive
You can also count the lines of all files within a directory and all of its subdirectories. This command may take a while if you are counting the lines of a lot of files.
$ linecount . -r # count all files in this directory + all subdirectories, recursively
$ linecount /path/to/dir -r # another way to specify the directory
Include Files
If you only want to count the lines of files containing a string in the filename, such as .py or hello, you can do:
$ linecount /path/to/dir -r --includefiles ".py,hello"
$ linecount /path/to/dir -r -if ".py,hello" # alternative way to write it
This will only include files whose names contain .py or hello. For example, the below files will be included:
hello.py
hello.txt
a.hello
cache.pyc
Exclude Files
If you want to exclude files containing a certain string in their name, for example, .txt, we can do:
$ linecount /path/to/dir -r --excludefiles ".txt"
$ linecount /path/to/dir -r -ef ".txt" # alternative way to write it
For example, this will exclude the following files:
myfile.txt
a.txt.py
Exclude Dirs
You can also exclude directories that have a specific string in their name. For example, if you want to exclude the venv directory:
$ linecount /path/to/dir -r --excludedirs "venv"
$ linecount /path/to/dir -r -ed "venv" # alternative way to write it
Combining Include Files and Exclude Files
If you want to include files that have the string .py, but not .pyc, you can include .py and exclude .pyc:
$ linecount /path/to/dir -if ".py" -ef ".pyc"
Python Usage
Importing
You can import linecount by performing:
import linecount as lc
The LineStats Tuple
The LineStats tuple is a common return value from line counting functions. It is a named tuple with five variables:
filepath: the path to the file the object is describinglines: the total number of lines in the filesouce_lines_of_code: the number of source lines of code (SLOC) in the filecommented_lines: the number of commented lines in the fileblank_lines: the number of empty lines in the file
Count the lines of a file
You can count the lines of a file with the lc.count_lines_file(filepath: str) function. It returns a LineStats named tuple.
line_stats: lc.LineStats = lc.count_lines_file("path/to/file.txt")
Counting the lines of a directory
The lc.count_lines_dir(dir_path: str) function counts the lines of all files within a directory. It takes one required argument, dir_path.
It returns a tuple of two values:
- The first value is a
LineStatsobject, and contains data on all the files in the directory summed up. - The second value is a
list[LineStats], and contains one entry per file that was counted.
It also takes in two optional arguments:
exclude_files: list[str]: excludes any files with filenames containing any of the strings listed. For example, ifexclude_filescontains[".py", ".txt"], all files containing.pyor.txtin their name will be excluded.include_files: list[str]: only filenames that contain one or more of the strings in the list will be included.
Usage example:
summary_stats, specific_stats = lc.count_lines_dir("path/to/dir", exclude_files=[".pyc"], include_files=[".py"])
Counting lines of files within a directory recursively
To recursively count the number of lines of files within a directory, the lc.count_lines_dir_recursive(dir_path: str) function can be used. It takes one required argument, dir_path.
It returns a tuple of two values:
- The first value is a
LineStatsobject, and contains data on all the files in the directory and subdirectories summed up. - The second value is a
list[LineStats]data structure, and contains one entry per file that was counted.
It contains three optional arguments:
exclude_files: list[str]: excludes any files with filenames containing any of the strings listed. For example, ifexclude_filescontains[".py", ".txt"], all files containing.pyor.txtin their name will be excluded.include_files: list[str]: only filenames that contain one or more of the strings in the list will be included.exclude_dirs: list[str]: directory names that contain one or more of the strings in the list will be excluded.
Example usage:
summary_stats, specific_stats = lc.count_lines_dir_recursive("path/to/dir", exclude_files=[".pyc"], include_files=[".py"], exclude_dirs=["venv"])
Release files for linecount 1.1.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| linecount-1.1.3.tar.gz | 9.5 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| linecount-1.1.3-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 18.2 kB
Release files / linecount-1.1.3.tar.gz
| Download URL | linecount-1.1.3.tar.gz |
|---|---|
| Size | 9.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
18b885437b40ea6b7837e693484e8c54c52752029f98add31f32e540b64ec632
|
|
BLAKE2b-256 checksum How to use checksums |
2802dc8f439122f808ac4b8a32fca5f387d66e5d736c5f20d3ec884d843954b0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.12.0
|
Release files / linecount-1.1.3-py3-none-any.whl
| Download URL | linecount-1.1.3-py3-none-any.whl |
|---|---|
| Size | 8.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
7842942e2be0caf798a51ac553a96cafcc8841bd25b55f86931fc5345302c443
|
|
BLAKE2b-256 checksum How to use checksums |
421709f1d7f47ba37388458020f85428be9db03c98c4863437c516e6ce058911
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.12.0
|