Package: linecount
Line CountA 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 oflinecountinclude:See the number of source lines of code (SLOC), commented lines, and blank linesExclude files or directories whose names contain certain characters. This is useful for excluding files with certain file extensions, or certain directories likevenvor.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 pagehereInstallingYou can install the project from pypi:$pipinstalllinecountCommand Line UsageHelpYou can get help on commands by performing:$linecount-h
$linecount--help# alternative way to write itCounting Lines of a File$linecountfile_to_count.txtCounting Lines in a Directory$linecount.# count all files in this directory$linecount/path/to/dir# or specify a directory this wayRecursiveYou 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 directoryInclude FilesIf you only want to count the lines of files containing a string in the filename, such as.pyorhello, you can do:$linecount/path/to/dir-r--includefiles".py,hello"$linecount/path/to/dir-r-if".py,hello"# alternative way to write itThis will only include files whose names contain.pyorhello.  For example, the below files will be included:hello.py
hello.txt
a.hello
cache.pycExclude FilesIf 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 itFor example, this will exclude the following files:myfile.txt
a.txt.pyExclude DirsYou can also exclude directories that have a specific string in their name. For example, if you want to exclude thevenvdirectory:$linecount/path/to/dir-r--excludedirs"venv"$linecount/path/to/dir-r-ed"venv"# alternative way to write itCombining Include Files and Exclude FilesIf you want to include files that have the string.py, but not.pyc, you can include.pyand exclude.pyc:$linecount/path/to/dir-if".py"-ef".pyc"Python UsageImportingYou can import linecount by performing:importlinecountaslcThe LineStats TupleTheLineStatstuple 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 fileCount the lines of a fileYou can count the lines of a file with thelc.count_lines_file(filepath: str)function. It returns aLineStatsnamed tuple.line_stats:lc.LineStats=lc.count_lines_file("path/to/file.txt")Counting the lines of a directoryThelc.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 aLineStatsobject, and contains data on all the files in the directory summed up.The second value is alist[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 recursivelyTo recursively count the number of lines of files within a directory, thelc.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 aLineStatsobject, and contains data on all the files in the directory and subdirectories summed up.The second value is alist[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"])