File Handling Worksheet
File Handling Worksheet
File Handling Worksheet
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.
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
*************