Advertisement
Guest User

Untitled

a guest
Jan 29th, 2020
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.09 KB | None | 0 0
  1. import os
  2. import argparse
  3. import glob
  4. import sys
  5.  
  6.  
  7. class Counter:
  8. #
  9. # This class allows to manage the class CounterLineAndCharacter
  10. #
  11.  
  12. def __init__(self, path):
  13. self.path = path
  14.  
  15.  
  16. def Run(self):
  17. if os.path.isfile(str(self.path)):
  18. Run = CounterLineAndCharacter(self.path)
  19. return Run.counterlinecharacter()
  20.  
  21.  
  22. elif os.path.isdir(str(self.path)):
  23. numberline = 0
  24. numbercharacter = 0
  25. listfile = listdirectory(self.path)
  26.  
  27. #
  28. # This loop allows to count the number of line, character of all file
  29. #
  30. for file in listfile:
  31. nl, nc = CounterLineAndCharacter(file).counterlinecharacter()
  32. numberline += nl
  33. numbercharacter += nc
  34.  
  35. #
  36. # This return the number of the line and of the character in all file
  37. #
  38. return numberline, numbercharacter
  39.  
  40.  
  41. else:
  42. raise FileNotFoundError('The file or the folder doesn\'t exists')
  43.  
  44.  
  45.  
  46. class CounterLineAndCharacter:
  47. #
  48. # This class allows to count the lines of a file
  49. #
  50.  
  51. def __init__(self, pathfile):
  52. self.pathfile = pathfile
  53. self.file = None
  54. self.numberline = None
  55. self.numbercharacter = None
  56.  
  57.  
  58. def counterlinecharacter(self):
  59. #
  60. # This fonction allows to count the number of the line in the file and the number of character
  61. # She returns a tuple of int
  62. #
  63. with open(self.pathfile, 'r', errors='ignore') as self.file:
  64. self.numberline = 0
  65. self.numbercharacter = 0
  66. for line in self.file:
  67. self.numbercharacter += len(line)
  68. if line != '':# Change here if you want ton a good number of line
  69. self.numberline += 1
  70.  
  71.  
  72. print('file > '+str(self.pathfile)+' line : '+str(self.numberline)+' || character : '+str(self.numbercharacter))
  73.  
  74. return self.numberline, self.numbercharacter
  75.  
  76.  
  77. def listdirectory(path):
  78. fichier=[]
  79. l = glob.glob(path+'/*')
  80. for i in l:
  81. if os.path.isdir(i):
  82. fichier.extend(listdirectory(i))
  83. else:
  84. fichier.append(i.replace('\\', '/'))
  85. return fichier
  86.  
  87.  
  88. if __name__ == '__main__':
  89. App = Counter(path=sys.argv[1])
  90. r1, r2 = App.Run()
  91. print('There are ', r1, 'lines', 'There are ', r2, 'characters')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement