Count the number of times a letter appears in a text file in python
<p>I'm aiming to create a <code>python</code> script that counts the number of times each letter appears from a text file. So if the text file contained <code>Hi there</code>, the output would be something like </p>

<pre><code>E is shown 2 times
H is shown 2 times
I is shown 1 time
R is shown 1 time
T is shown 1 time
</code></pre>

<p>I've tried different ways of getting this but I have no output being shown as I carry on getting syntax errors. I've tried the following</p>

<pre><code>import collections
import string

def count_letters(example.txt, case_sensitive=False):
    with open(example.txt, 'r') as f:
        original_text = f.read()
    if case_sensitive:
        alphabet = string.ascii_letters
        text = original_text
    else:
        alphabet = string.ascii_lowercase
        text = original_text.lower()
    alphabet_set = set(alphabet)
        counts = collections.Counter(c for c in text if c in alphabet_set)

    for letter in alphabet:

        print(letter, counts[letter])
    print("total:", sum(counts.values()))

    return counts
</code></pre>

<p>And </p>

<pre><code>def count_letters(example.txt, case_sensitive=False):
    alphabet = "abcdefghijlkmnopqrstuvxyzABCDEFGHIJKLMNOPQRSTUVXYZ"
    with open(example.txt, 'r') as f:
        text = f.read()
    if not case_sensitive:
        alpahbet = alphabet[:26]
        text = text.lower()
        letter_count = {ltr: 0 for ltr in alphabet}
    for char in text:
        if char in alphabet:
            letter_count[char] += 1
    for key in sorted(letter_count):
        print(key, letter_count[key])
    print("total", sum(letter_count())) 
</code></pre>