Python Program to Count Frequency of Characters in a Given File

Aim:

Write a program to count the frequency of characters in a given file.

Can you use character frequency to tell whether the given file is a Python program file, C program file or a text file?

Program:

import os
count =0
file=open("D:/a.txt")
for line in file:
 for l in range(0,len(line)):
 count+=1;
print("count:",count)
filename,file_extension=os.path.splitext("D:/a.txt"); 
print("file_extension==",file_extension);
if(file_extension=='.py'):
 print("its python program file");
elif(file_extension==".txt"):
 print("its a txt file");
elif(file_==extension==".c"):
print("its a c program file"); 

Output:

 a.txt contain “hi”

count: 2
file_extension== .txt
its a txt file 

Leave a Comment