Write a program that reads a text file and counts the occurrences of each alphabet in the file. The program should prompt the user to enter the filename in python

Code

filename = input("Enter file name: ")

# open file in read mode
f = open(filename, "r")

# read the file
data = f.read()

# count the occurrences of characters
count = {}
for char in data:
if char != " ":
if char in count:
count[char] += 1
else:
count[char] = 1
# print the result

for key, value in count.items():
print(f"{key}: {value}")


Output

Enter file name: test.txt
h: 1
e: 1
l: 3
o: 2
w: 1
r: 1
d: 1