Read the file. Store the content of the file in a variable. Use the count() method with the argument as a letter whose frequency is required. Display the count of the letter.
# Program to get letter count in a text file
# explit function to return the letter count def letterFrequency(fileName, letter): # open file in read mode file = open(fileName, 'r')
# store content of the file in a variable text = file.read()
# using count() return text.count(letter)
# call the function and display the letetr count print(letterFrequency('gfg.txt', 'g'))
Read the file.
Store the content of the file in a variable.
Use the count() method with the argument as a letter whose frequency is required.
Display the count of the letter.
# Program to get letter count in a text file
# explit function to return the letter count
def letterFrequency(fileName, letter):
# open file in read mode
file = open(fileName, 'r')
# store content of the file in a variable
text = file.read()
# using count()
return text.count(letter)
# call the function and display the letetr count
print(letterFrequency('gfg.txt', 'g'))