12345678910111213141516171819202122232425262728293031 |
- import argparse
- from collections import namedtuple
- import logging
- logger = logging.getLogger(__name__)
- FileSize = namedtuple("FileSize", ["lines", "words"])
- class FileSizeCounter:
- def count(self, input_path):
- logger.info("Counting size of {0}.".format(input_path))
- with open(input_path) as input_file:
- return self.count_sizes(input_file)
- def count_sizes(self, input_file):
- line_count = 0
- word_count = 0
- for line in input_file:
- line_count += 1
- word_count += len(line.strip().split())
- return FileSize(lines=line_count, words=word_count)
- if __name__ == "__main__":
- argparser = argparse.ArgumentParser()
- argparser.add_argument("input_path", help="path to input file")
- args = argparser.parse_args()
- print(FileSizeCounter().count(args.input_path))
|