Advertisement
jille_Jr

Python3: Count lines of code

Mar 7th, 2019
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.04 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. # This Python script counts the lines of code in the directory in which it is
  4. # run.  It only looks at files which end in the file extensions passed to the
  5. # script as arguments.
  6.  
  7. # It outputs counts for total lines, blank lines, comment lines and code lines
  8. # (total lines minus blank lines and comment lines).
  9.  
  10. # Example usage and output:
  11. # > lines_of_code_counter.py .h .cpp
  12. # Total lines:   15378
  13. # Blank lines:   2945
  14. # Comment lines: 1770
  15. # Code lines:    10663
  16.  
  17. # Change this value based on the comment symbol used in your programming
  18. # language.
  19. commentSymbols = {
  20.     ".cs": "//",        # C#
  21.     ".js": "//",        # JavaScript
  22.     ".sh": "#",         # Bash
  23.     ".ps1": "#",        # Powershell
  24.     ".py": "#",         # Python
  25.     ".Dockerfile": "#", # Docker
  26.     ".md": "<!--",      # Markdown
  27.     ".c": "//",         # C
  28.     ".h": "//",         # C & C++ headers
  29.     ".cpp": "//",       # C++
  30. }
  31.  
  32. import sys
  33. import os, os.path
  34.  
  35. acceptableFileExtensions = sys.argv[1:]
  36. if not acceptableFileExtensions:
  37.     print('Please pass at least one file extension as an argument.')
  38.     quit()
  39.  
  40. currentDir = os.getcwd()
  41.  
  42. filesToCheck = []
  43. for root, _, files in os.walk(currentDir):
  44.     for f in files:
  45.         fullpath = os.path.join(root, f)
  46.         if '.git' not in fullpath:
  47.             for extension in acceptableFileExtensions:
  48.                 if fullpath.endswith(extension):
  49.                     filesToCheck.append(fullpath)
  50.  
  51. if not filesToCheck:
  52.     print('No files found.')
  53.     quit()
  54.  
  55. lineCount = 0
  56. totalBlankLineCount = 0
  57. totalCommentLineCount = 0
  58.  
  59. print('')
  60. print('Filename\tlines\tblank lines\tcomment lines\tcode lines')
  61.  
  62. for fileToCheck in filesToCheck:
  63.     with open(fileToCheck) as f:
  64.  
  65.         fileLineCount = 0
  66.         fileBlankLineCount = 0
  67.         fileCommentLineCount = 0
  68.  
  69.         fileName, fileExtension = os.path.splitext(fileToCheck)
  70.         assert fileExtension in commentSymbols, "Unknown comment symbol for extension '%s'" % fileExtension
  71.         commentSymbol = commentSymbols[fileExtension]
  72.  
  73.         for line in f:
  74.             lineCount += 1
  75.             fileLineCount += 1
  76.  
  77.             lineWithoutWhitespace = line.strip()
  78.             if not lineWithoutWhitespace:
  79.                 totalBlankLineCount += 1
  80.                 fileBlankLineCount += 1
  81.             elif lineWithoutWhitespace.startswith(commentSymbol):
  82.                 totalCommentLineCount += 1
  83.                 fileCommentLineCount += 1
  84.  
  85.         print(os.path.basename(fileToCheck) + \
  86.               "\t" + str(fileLineCount) + \
  87.               "\t" + str(fileBlankLineCount) + \
  88.               "\t" + str(fileCommentLineCount) + \
  89.               "\t" + str(fileLineCount - fileBlankLineCount - fileCommentLineCount))
  90.  
  91.  
  92. print('')
  93. print('Totals')
  94. print('--------------------')
  95. print('Lines:         ' + str(lineCount))
  96. print('Blank lines:   ' + str(totalBlankLineCount))
  97. print('Comment lines: ' + str(totalCommentLineCount))
  98. print('Code lines:    ' + str(lineCount - totalBlankLineCount - totalCommentLineCount))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement