Python Count Certain Character
Question: 
    def file(char, filename):
        for currentFile in filename:
            print(currentFile.strip())
    
    def string(char, str):
        count = 0
        if char in 'abcdefghijklmnopqrstuvwxyz':
            count += 1
            string(char,str)
        else:
            print("Incorrect Letters")
        print(count)
    
    def main():
        char = input("Enter character: ")
        openFile = input("Enter the filename: ")
    
        filename = open(openFile)
    
        file(char, filename)
        string(char, str)
    
    main()
    

I am trying to count certain character, for example, if I was to put "W" in
the char input prompt, It should count only W. How would I do that? I am
trying to do the recursion in the def string function

Thank you.


Answer: Here is a solution without recursion and regular expressions, just using
built-ins.

    
    
    import sys
    
    char = raw_input("Enter character: ")
    # 'isalpha' does the same as your manual check and is more idiomatic
    if not char.isalpha():
        print "Incorrect letters"
        # This will terminate the script
        sys.exit()
    
    
    fname = raw_input("Enter filename: ")
    
    count = 0
    
    # We use a context manager to open a file, this way we don't
    # have to close it ourselves when we're done. This is the idiomatic
    # way to open files in Python since context managers were introduced.
    with open(fname, 'r') as fp:
        # We go through the file line by line
        for line in fp:
            # We can use the built-in 'count' method to count
            # the occurences of a character in a string
            # Use 'line.lower().count(char)' if you want to be case-insensitive
            count += line.count(char)
    
    print count
    

