file_size_counter.py 859 B

12345678910111213141516171819202122232425262728293031
  1. import argparse
  2. from collections import namedtuple
  3. import logging
  4. logger = logging.getLogger(__name__)
  5. FileSize = namedtuple("FileSize", ["lines", "words"])
  6. class FileSizeCounter:
  7. def count(self, input_path):
  8. logger.info("Counting size of {0}.".format(input_path))
  9. with open(input_path) as input_file:
  10. return self.count_sizes(input_file)
  11. def count_sizes(self, input_file):
  12. line_count = 0
  13. word_count = 0
  14. for line in input_file:
  15. line_count += 1
  16. word_count += len(line.strip().split())
  17. return FileSize(lines=line_count, words=word_count)
  18. if __name__ == "__main__":
  19. argparser = argparse.ArgumentParser()
  20. argparser.add_argument("input_path", help="path to input file")
  21. args = argparser.parse_args()
  22. print(FileSizeCounter().count(args.input_path))