renix1

words and chars in text file

Jan 26th, 2017
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.63 KB | None | 0 0
  1. from os import listdir, path, getcwd
  2.  
  3. def count_words(texto):
  4.     return len(texto.split())
  5.  
  6. def count_chars(texto):
  7.     return len(list(texto))
  8.  
  9. def main():
  10.     files = [f for f in listdir(getcwd()) if f.endswith('.txt')]
  11.     for file in files:
  12.         print(" START ".center(80, 'x'))
  13.         total_chars, total_words = 0, 0
  14.         with open(file) as f:
  15.             lines = f.readlines()
  16.             for line in lines:
  17.                 total_chars += count_chars(line)
  18.                 total_words += count_words(line)
  19.         print("(%s) Total characteres: %d\n(%s) Total words: %s\n" % (file, total_chars, file, total_words))
  20.         print(' END '.center(80, 'x'))
  21.  
  22. if __name__ == '__main__':
  23.     main()
Advertisement
Add Comment
Please, Sign In to add comment