TEXT FILE HANDLING Questions
TEXT FILE HANDLING Questions
1. Write a user defined function in python that displays the number of lines starting with 'H' in
the file para.txt.
2. Write a function countmy() in python to write the text file "DATA.TXT" and count the
number of times "my" occurs in the file. For example if the file DATA.TXT contains-"This is
my website. I have displayed my preference in the CHOICE section ".-the countmy() function
should display the output as:"my occurs 2 times".
3. Write a method in python to read lines from a text file DIARY.TXT and display those lines
which start with the alphabets P.
4. Write a method in python to read lines from a text file MYNOTES.TXT and display those
lines which start with alphabets 'K'.
5. Write a program that copies a text file "source.txt" onto "target.txt" barring the lines starting
with @ sign.
6. Write a function in Python to read a text file, Alpha.txt and displays those lines which begin
with the word ‘You’.
7. Write a function, vowelCount() in Python that counts and displays the number of vowels in the
text file named Poem.txt.
8. Read a text file line by line and display each word separated by a #.
10. Remove all the lines that contain the character 'a' in a file and write it to another file.
11. Write a function COUNT_AND( ) in Python to read the text file “STORY.TXT” and
count the number of times “AND” occurs in the file. (include AND/and/And in the
counting)
12. Write a function DISPLAYWORDS( ) in python to display the count of words starting
with “t” or “T” in a text file ‘STORY.TXT’.
13. Write a function in python to read lines from file “POEM.txt” and display all
those words, which has two characters in it.
For e.g. if the content of file is
O Corona O Corona
Jaldi se tum Go na
Social Distancing ka palan karona
14. Write a function COUNT() in Python to read contents from file “REPEATED.TXT”, to
count and display the occurrence of the word “Catholic” or “mother”.
For example:
If the content of the file is
“Nory was a Catholic because her mother was a Catholic, and Nory‟s mother was a Catholic
because her father was a Catholic, and her father was a Catholic because his mother was a
Catholic , or had been.
The function should display:
Count of Catholic, mother is 9
15. Write a function that counts and display the number of 5 letter words in a
text file “Sample.txt”
16. Write a function to display those lines which start with the letter “S” from
the text file “MyNotes.txt”.
19. Write a function in Python that counts the number of “Me” or “My” words present in a text
file
“STORY.TXT”. If the “STORY.TXT” contents are as follows:
My first book was Me and My Family.
It gave me chance to be Known to the world.
21. Write a function in python that displays the number of lines starting with ‘H’
in the file
“para.txt”. Example, if file contains:
Whose woods these are I think I know.
His house is in the village though;
He will not see me stopping here
To watch his woods fill up with snow
Then the lines count should be 2
22. Write a function countmy() in Python to read file Data.txt and count the
number of times “my” occur in file. For example, if the file contain
This is my website. I have displayed my preferences in the choice section
The countmy() function should display the output as :” my occurs 2 times”
23. Write a Python program to find the number of lines in a text file ‘abc.txt’.
24. Write a Python program to count the word “if “ in a text file abc.txt’.
25. Write a function in Python that counts the number of “Me” or “My” words 3
present in a text file “STORY.TXT”.
If the “STORY.TXT” contents are as follows:
My first book was Me and My Family. It gave me chance to be Known to the world.
The output of the function should be:
Count of Me/My in file: 4
27. Write a method in python to read lines from a text file DIARY.TXT and display
those lines which start with the alphabets P.
28. Write a function countmy( ) in python to read the text file "mystory.txt" and count
the number of times "my" occurs in the file. For example if the file mystory.txt
contains:
"This is my school. I love to play and study in my school."
the countmy( ) function should display the output as:"my occurs 2 times".
29. Write a method/function ISTOUPCOUNT() in python to read contents from a text file
WRITER.TXT, to count and display the occurrence of the word ‘‘IS’’ or ‘‘TO’’ or ‘‘UP’’.
For example : If the content of the file is-
IT IS UP TO US TO TAKE CARE OF OUR SURROUNDING. IT IS NOT POSSIBLE
ONLY FOR THE GOVERNMENT TO TAKE RESPONSIBILITY
The method/function should display Count of IS TO and UP is 6
30. Write a function AMCount() in Python, which should read each character of a text
file STORY.TXT, should count and display the occurrence of alphabets A and M
(including small cases a and m too).
Example:
If the file content is as follows:
Updated information
As simplified by official websites.
The AMCount() function should display the output as:
A or a:4
31. Write a function in Python that counts the number of “The” or “This”
words present in a text file “MY_TEXT_FILE.TXT”.
Note: (The comparison should be case insensitive)
33. Write a function count_Dwords() in python to count the words ending with a digit in a text
file “Details.txt”.
Example: If the file content is as follows:
On seat2 VIP1 will sit and
On seat1 VVIP2 will be sitting
Output will be:
Number of words ending with a digit are 4