Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- #countLines.py
- if __name__ != "__main__": exit(0)
- from time import time
- from os import walk as folderIterate
- from os.path import isdir, exists, join as pathJoin, relpath, getsize
- from sys import argv
- def countLinesInFolderRecursively(rootIn):
- lineCountFolder=0
- byteCountFolder=0
- if not exists(rootIn): return None
- if not isdir(rootIn): return False
- for root, dirs, files in folderIterate(rootIn,topdown=True):
- currentRel=relpath(root,rootIn)
- for fi in files:
- fileInPath=pathJoin(root,fi)
- with open(fileInPath,"r") as file:
- lineCountFile=len(open(fileInPath).readlines())
- byteCountFile=getsize(fileInPath)
- lineCountFolder+=lineCountFile
- byteCountFolder+=byteCountFile
- print("{}: {} lines, ({} Bytes)".format(fileInPath,lineCountFile,byteCountFile))
- return lineCountFolder,byteCountFolder
- if len(argv) < 2: exit()
- lCTInst,bCTInst=0,0
- lineCountTotal=0
- byteCountTotal=0
- for i in range(1,len(argv)):
- lCTInst,bCTInst=countLinesInFolderRecursively(argv[i])
- lineCountTotal+=lCTInst
- byteCountTotal+=bCTInst
- print("line count is now {} lines total! ({:.2f} kiB)".format(lineCountTotal,byteCountTotal/1024))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement