10 Important Questions of Text File Handling in Python

Share with others

Important Questions of File Handling in Python from previous years Sample Paper

Q1. Write a function in Python that counts the number of “Me” or “My” (in smaller case also) 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.

The output of the function should be: Count of Me/My in file: 4

Solution

Important Questions of File Handling in Python

Q2. Write a function AMCount() in Python, which should read each character of a text file STORY.TXT, should count and display the occurence 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, M or m =2

Solution

Important Questions of File Handling in Python

Q3. Write a function in python to count the number of lines in a text file ‘

STORY.TXT’ which is starting with an alphabet ‘A’ .

Solution:

Important Questions of File Handling in Python

Q4. Write a method/function DISPLAYWORDS() in python to read lines from a text file STORY.TXT, and display those words, which are less than 4 characters.

Solution

Important Questions of File Handling in Python

Q5. Write a function RevText() to read a text file “ Story.txt “ and Print only word starting with ‘I’ in reverse order . Example: If value in text file is: INDIA IS MY COUNTRY Output will be: AIDNI SI MY COUNTRY.

Solution

Important Questions of File Handling in Python

Q6. Write a function in python to count the number of lowercase alphabets present in a text file “Story.txt”

Solution

Important Questions of File Handling in Python

Q7. Write a user-defined function named count() that will read the contents of text file named “Story.txt” and count the number of lines which starts with either “I‟ or “M‟. E.g. In the following paragraph, there are 2 lines starting with “I‟ or “M‟:

“India is the fastest growing economy. 
India is looking for more investments around the globe. 
The whole world is looking at India as a great market. 
Most of the Indians can foresee the heights that India is capable of reaching.”

Solution

Important Questions of File Handling in Python

Q8. Write a function countmy( )in Python to read the text file “Story.txt” and count the number of times “my” or “My” occurs in the file. For example if the file “Story.TXT” contains:

“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”.

Solution

Important Questions of File Handling in Python

Q9. Write a user defined function countwords() in python to count how many words are present in a text file named “story.txt”. For example, if the file story.txt contains following text:

Co-education system is necessary for a balanced society. With co-education system, Girls and Boys may develop a feeling of mutual respect towards each other.

The function should display the following:

Total number of words present in the text file are: 24

Solution

Important Questions of File Handling in Python

Q10. Write a user defined function in Python that displays the number of lines starting with ‘H’ in the file story.txt. Eg: if the 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 line count should be 2

Solution

Important Questions of File Handling in Python

After doing the above 10 Important Questions of File Handling in Python, we can conclude that following  types of questions are important Questions of File Handling in Python (Board Exam)
  • Count the total words in file.
  • Count the total number of lines.
  • Count the lines start from a specific character.
  • Count the frequency of a particular character or a word or words.
I hope you will like this collection of Important Questions of File Handling in Python and in case of any query you can contact directly to csiplearninghub@gmail.com

Related Post

10 Important Questions of Binary File Handling in Python

10 Important Questions of CSV File Handling in Python

Handout of Text File Handling in Python

40+ Important Python File Handling Practice Questions

Handout of Binary File Handling in Python

Disclaimer : Above are the 10 Important Questions of File Handling in Python from previous years Sample Paper. In few questions I changed the name of the file and also changed from C++ to python.

Class 12 Computer Science Sample Paper 2020-2021.

Class 12 Computer Science Sample Paper Marking Scheme

Class 12 Computer Science Test Series


Share with others

Leave a Reply

error: Content is protected !!