Advertisement
Kitomas

countLines.py

Aug 4th, 2023
1,028
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.28 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. #countLines.py
  3. if __name__ != "__main__": exit(0)
  4. from time import time
  5. from os import walk as folderIterate
  6. from os.path import isdir, exists, join as pathJoin, relpath, getsize
  7. from sys import argv
  8.  
  9. def countLinesInFolderRecursively(rootIn):
  10.     lineCountFolder=0
  11.     byteCountFolder=0
  12.     if not exists(rootIn): return None
  13.     if not isdir(rootIn): return False
  14.     for root, dirs, files in folderIterate(rootIn,topdown=True):
  15.         currentRel=relpath(root,rootIn)
  16.         for fi in files:
  17.             fileInPath=pathJoin(root,fi)
  18.             with open(fileInPath,"r") as file:
  19.                 lineCountFile=len(open(fileInPath).readlines())
  20.                 byteCountFile=getsize(fileInPath)
  21.                 lineCountFolder+=lineCountFile
  22.                 byteCountFolder+=byteCountFile
  23.                 print("{}: {} lines, ({} Bytes)".format(fileInPath,lineCountFile,byteCountFile))
  24.     return lineCountFolder,byteCountFolder
  25.  
  26. if len(argv) < 2: exit()
  27. lCTInst,bCTInst=0,0
  28. lineCountTotal=0
  29. byteCountTotal=0
  30. for i in range(1,len(argv)):
  31.     lCTInst,bCTInst=countLinesInFolderRecursively(argv[i])
  32.     lineCountTotal+=lCTInst
  33.     byteCountTotal+=bCTInst
  34. print("line count is now {} lines total! ({:.2f} kiB)".format(lineCountTotal,byteCountTotal/1024))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement