Advertisement
DigitalMag

lines counter

Dec 4th, 2021
1,020
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.27 KB | None | 0 0
  1. import os
  2. import sys
  3. import glob
  4.  
  5. excepts = {
  6.     'files': {
  7.         'exts': ['.sqlite3', '.png', '.jpeg', '.pyc'],
  8.     },
  9.     'dirs': [
  10.         '.git',
  11.         'node_modules',
  12.         '__pycache__'
  13.     ]
  14. }
  15.  
  16.  
  17. lines = {ext: {} for ext in ['py', 'html']}
  18.  
  19. # glob.glob()
  20.  
  21. def main():
  22.     print('-'* 50)    
  23.     print('')
  24.     root_dir = os.path.abspath(os.curdir) if len(sys.argv) > 0 else sys.argv[1]
  25.     print('starts scan: ' + root_dir + '\n')    
  26.  
  27.     def get_lines_in_files(files, _dir = ''):
  28.         for file in files:
  29.             with open(file, 'r') as eyes:
  30.                 # print(file)
  31.                 ext = file.split('.').pop()
  32.                 if ext in lines.keys():
  33.                     lines[ext][(file + (f'__{_dir}') if _dir else file)] = len(eyes.readlines())
  34.  
  35.     # root_dirs = os.listdir(root_dir)
  36.     def get_lines_in_folder(path_name):
  37.         for root, dirs, files in os.walk(root_dir):
  38.             get_lines_in_files(files)
  39.        
  40.  
  41.     get_lines_in_folder(root_dir)
  42.  
  43.     for _ext, _files in lines.items():
  44.         _sum = sum(_files.values())
  45.         print(_ext.upper() + f': (total {_sum} lines)\n')
  46.         for _file, count in _files.items():        
  47.             print(f"{_file}: {count}")
  48.  
  49. if __name__ == '__main__':
  50.     main()
  51.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement