w3resource logo


Python Exercises

Python File I/O: Count the frequency of words in a file

Python File I/O: Exercise-10 with Solution

Write a Python program to count the frequency of words in a file.

Contain of text.txt

What is Python language?                                                
Python is a widely used high-level, general-purpose, interpreted, dynamic programming language.                                                 
Its design philosophy emphasizes code readability, and its syntax allows programmers to express concepts in fewer lines of code than possible in 
languages such as C++ or Java.                                         
Python supports multiple programming paradigms, including object-oriented, imperative and functional programming or procedural styles.          
It features a dynamic type system and automatic memory management and has a large and comprehensive standard library.                           
The best way we learn anything is by practice and exercise questions. We  have started this section for those (beginner to intermediate) who are 
familiar with Python.

Output :

Number of words in the file : Counter({'and': 6, 'a': 3, 'Python': 3, 'is': 3, 'programming': 3,
'or': 2, 'code': 2, 'in': 2, 'dynamic': 2, 'to': 2, 'such': 1, 'including': 1, '(beginner': 1,
'allows': 1,'The': 1, 'automatic': 1, 'syntax': 1, 'best': 1, 'started': 1, 'Python.': 1,
'styles.': 1, 'of': 1,'What': 1, 'procedural': 1, 'anything': 1, 'than': 1, 'type': 1, 'features': 1,
 'It': 1, 'have': 1, 'we': 1, 'exercise': 1, 'who': 1, 'by': 1, 'C++': 1, 'library.': 1, 'those': 1,
'management': 1, 'large': 1, 'intermediate)': 1, 'for': 1, 'section': 1, 'possible': 1, 'lines': 1,
'We': 1, 'Its': 1, 'design': 1, 'programmers': 1, 'languages': 1, 'has': 1, 'philosophy': 1,
'multiple': 1, 'practice': 1, 'are': 1, 'familiar': 1, 'functional': 1, 'with': 1, 'memory': 1,
'express': 1, 'comprehensive': 1, 'used': 1, 'interpreted,': 1, 'readability,': 1, 'its': 1,
'emphasizes': 1, 'fewer': 1, 'paradigms,': 1, 'general-purpose,': 1, 'widely': 1, 'imperative': 1,
'concepts': 1, 'standard': 1, 'as': 1, 'this': 1, 'language?': 1, 'learn': 1,'object-oriented,': 1,
'system': 1, 'high-level,': 1, 'supports': 1, 'Java.': 1, 'questions.': 1, 'way': 1, 'language.': 1}) 

Sample Solution :-

Python Code :

from collections import Counter
def word_count(fname):
        with open(fname) as f:
                return Counter(f.read().split())

print("Number of words in the file :",word_count("test.txt"))

Python Code Editor:

from collections import Counter
def word_count(fname):
        with open(fname) as f:
                return Counter(f.read().split())

print("Number of words in the file :",word_count("test.txt"))

Improve this sample solution and post your code through Disqus



Join our Question Answer community to learn and share your programming knowledge.

Solve these problems:

Java: How to convert a string to an integer in Java?

C#: Loops in c#

SQL: JOIN using more than 5 tables