Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import argparse
- import glob
- import sys
- class Counter:
- #
- # This class allows to manage the class CounterLineAndCharacter
- #
- def __init__(self, path):
- self.path = path
- def Run(self):
- if os.path.isfile(str(self.path)):
- Run = CounterLineAndCharacter(self.path)
- return Run.counterlinecharacter()
- elif os.path.isdir(str(self.path)):
- numberline = 0
- numbercharacter = 0
- listfile = listdirectory(self.path)
- #
- # This loop allows to count the number of line, character of all file
- #
- for file in listfile:
- nl, nc = CounterLineAndCharacter(file).counterlinecharacter()
- numberline += nl
- numbercharacter += nc
- #
- # This return the number of the line and of the character in all file
- #
- return numberline, numbercharacter
- else:
- raise FileNotFoundError('The file or the folder doesn\'t exists')
- class CounterLineAndCharacter:
- #
- # This class allows to count the lines of a file
- #
- def __init__(self, pathfile):
- self.pathfile = pathfile
- self.file = None
- self.numberline = None
- self.numbercharacter = None
- def counterlinecharacter(self):
- #
- # This fonction allows to count the number of the line in the file and the number of character
- # She returns a tuple of int
- #
- with open(self.pathfile, 'r', errors='ignore') as self.file:
- self.numberline = 0
- self.numbercharacter = 0
- for line in self.file:
- self.numbercharacter += len(line)
- if line != '':# Change here if you want ton a good number of line
- self.numberline += 1
- print('file > '+str(self.pathfile)+' line : '+str(self.numberline)+' || character : '+str(self.numbercharacter))
- return self.numberline, self.numbercharacter
- def listdirectory(path):
- fichier=[]
- l = glob.glob(path+'/*')
- for i in l:
- if os.path.isdir(i):
- fichier.extend(listdirectory(i))
- else:
- fichier.append(i.replace('\\', '/'))
- return fichier
- if __name__ == '__main__':
- App = Counter(path=sys.argv[1])
- r1, r2 = App.Run()
- print('There are ', r1, 'lines', 'There are ', r2, 'characters')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement