num_chars = 0
with open(fname, 'r') as f:
    for line in f:
        words = line.split()
num_lines += 1
num_words += len(words)
num_chars += len(line)
Counting lines, words, and characters within a text file using Python