File Handling Worksheet

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

CHENNAI PUBLIC SCHOOL

Anna Nagar West, Chennai, 600101

Grade 12 - Computer Science


Data File Handling Worksheet

Programming based Questions:


1. Write a user defined function to display the total number of
words present in the file. A text file “Quotes.Txt” has the following
data written in it:
Living a life you can be proud of doing your best. Spending your
time with people and activities that are important to you Standing
up for things that are right even when it’s hard becoming the best
version of you.
The countwords() function should display the output as:
Total number of words : 40

2. Write a function c_words () in Python that separately counts


and displays the number of vowels and consonants in a text file
words.txt.
3. Write a function in python to count the number lines in a text
file “Country.txt” which is starting with an alphabet “W” or “H”. If the
file contents are as follows:
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.
The output of the function should be:
W or w : 1
H or h : 2
4. Write a user defined function in Python named showlines()
which reads contents of a text file named story.txt and displays
every sentence in a separate line.
Assume that a sentence ends with a full stop (.), a question mark
(?), or an exclamation mark (!).

5. Write a program to get roll numbers, names and marks of the


students of a class (get from user as input) and store these details
in a file called “Marks.dat”.

6. Write a program to add two more students details to the file


created in program 1.

7. Write a program to display the contents of file “Marks.dat”


created through program 4 and program 5.

8. A file sports.dat contains information in following format :


Event - Participant
Write a function that would read contents from file sports.dat and
creates a file named “Football.dat” copying only those records from
sports.dat where the event name is "Football".

9. A file contains a list of telephone numbers in the following


form:
Arvind 7258031
Sachin 7259197
The names contain only one word and the names and telephone
numbers are separated by a space. Write program to read a file
and display its contents in two columns.

10. Write a program to count the words "to" and "the" present in a
text file "Poem.txt".

11. Write a program that reads characters from the keyboard one
by one. All lower case characters get stored inside the file
LOWER.txt, all upper case characters get stored inside the file
UPPER.txt and all other characters get stored inside file
OTHERS.txt.

12. Write a function in Python to count and display the number of


lines starting with alphabet 'A' present in a text file " LINES.TXT".
e.g., the file "LINES.TXT" contains the following lines :
A boy is playing there.
There is a playground.
An aeroplane is in the sky.
The function should display the output as 3.

13. A text file named CONTENTS. TXT contains some text. Write a
user-defined function LongWords() in PYTHON which displays all
such words of the file whose length is more than 9 alphabets.
For example: if the file CONTENTS. TXT contains:
"Conditional statements of PYTHON programming language are if
and elif"
Then the function LongWords() should display the output as:
Conditional
statements
programming

14. A text file named MESSAGE. TXT contains some text. Another
text file named SMS. TXT needs to be created such that it would
store only the first 150 characters from the file MESSAGE. TXT.
Write a user-defined function LongToShort() in PYTHON that would
perform the above task of creating SMS. TXT from the already
existing file MESSAGE. TXT.

15. A text file named WORDS. TXT contains some text. Write a
user-defined function MAGICWORDS() in PYTHON to read and
display those words, which is starting with alphabet ‘A’ (irrespective
of upper or lower case).
For example: if the file WORDS.TXT contains:
A lot of adorable cute dolls were displayed in Showbiz festival.
Anya had boxes of Pizzas in her hand
The function should display:
A
adorable
Anya

16. A text file named DRAFT. TXT contains some text. Write a
user-defined function MakeNew() in PYTHON, which transfers lines
from DRAFT. TXT to FINAL. TXT, which are not starting with
alphabet ‘X’.
For example: if the file DRAFT. TXT contains:
Completed 3 chapters of Chemistry
XCompleted all chapters of English
Completed 4 chapters of Physics
Completed 5 chapters of English
Then the function MakeNew() should transfer the following lines to
FINAL. TXT:
Completed 3 chapters of Chemistry
Completed 4 chapters of Physics
Completed 5 chapters of English

17. A text file named MATTER. TXT contains some text, which
needs to be displayed such that every next character is separated
by a symbol ‘#’. Write a function definition for HashDisplay() in
PYTHON that would display the entire content of the file MATTER.
TXT in the desired format.
Example:
If the file MATTER.TXT has the following content stored in it:
THE WORLD IS ROUND
The function HashDisplay() should display the following content:
T#H#E# #W#O#R#L#D# #I#S# #R#O#U#N#D#

18. Aditi has used a text editing software to type some text. After
saving the article as WORDS. TXT, she realised that she has
wrongly typed alphabet J in place of alphabet I everywhere in the
article. Write a function definition for JTOI() in PYTHON that would
display the corrected version of entire content of the file WORDS.
TXT with all the alphabets “J” to be displayed as an alphabet “I”
on screen.
Note: Assuming that WORD. TXT does not contain any J alphabet
other than the typing error.
Example:
If Aditi has stored the following content in the file WORDS. TXT:
WELL, THJS JS A WORD BY JTSELF. YOU COULD STRETCH
THJS TO BE A SENTENCE
The function JTOI() should display the following content:
WELL, THIS IS A WORD BY ITSELF. YOU COULD STRETCH
THIS TO BE
A SENTENCE

19. Write function definition for DISP3CHAR() in PYTHON to read


the content of a text file KIDINME.TXT, and display all those words,
which have three characters in it.

Example: If the content of the file KIDINME.TXT is as follows:


When I was a small child, I used to play in the garden
with my grand mom. Those days were amazingly funful and
I remember all the moments of that time

The function DISP3CHAR() should display the following:


was the mom and all the

20. Write a function RevText() to read a text file “Story.txt” and


Print only word starting with 'I' in reverse order.

*************

You might also like