Python Program to Count Tabs, Spaces and Newline Characters in a File
x
21
21
1
file1=open("AI.txt", "w")
2
file1.write("Artificial Intelligence is a method of learning new data.
3
\n Artificial Intelligence covers three types of tasks:\n 1. Mundane Tasks\t
4
2. Formal Tasks\t 3. Expert Tasks")
5
file1=open("AI.txt", "r")
6
filename=input("Enter the filename: ")
7
with open(filename) as file:
8
text=file.read()
9
count_tab=0
10
count_space=0
11
count_newline=0
12
for char in text:
13
if char=='\t':
14
count_tab+=1
15
if char==' ':
16
count_space+=1
17
if char=='\n':
18
count_newline+=1
19
print("How many Tabs are present in these file? ", count_tab)
20
print("How many Spaces are present in these file? ", count_space)
21
print("How many Newlines are present in these file? ", count_newline)