In a python module, define two functions:
Answer: Python module called functions
def read_file_content(filename):
file = open(filename, 'r')
file_content = file.read();
words = file_content.split(' ')
return words
def find_number_of_words(filename):
words = read_file_content(filename)
return len(words)
def count_occurences(words, word):
occurences = 0
for i in range(len(words)):
if words[i] == word:
occurences+=1
return occurences
Main module:
import os.path
from os import path
from functions import *
filename = str(input("Enter filename: "))
if path.exists(filename):
word = str(input("Enter word: "))
words = read_file_content(filename)
occurences = count_occurences(words, word)
if occurences == 0:
print("Word not found")
else:
print("{0} occurences".format(occurences))
else:
print("Error! File doesn't exist!")
For testing, please create a file called input.txt and put the following text: hello all today is Monday
Comments
Leave a comment