0% found this document useful (0 votes)
28 views

Exercise of File Handling

The document outlines a series of Python programming exercises focused on file handling, including reading from text and binary files, counting specific characters or words, and manipulating file content. It includes tasks such as displaying file content, counting lines, words, and occurrences of specific letters, as well as creating and modifying binary files. The exercises are designed to enhance understanding of file operations in Python.

Uploaded by

gbbloodshot41
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Exercise of File Handling

The document outlines a series of Python programming exercises focused on file handling, including reading from text and binary files, counting specific characters or words, and manipulating file content. It includes tasks such as displaying file content, counting lines, words, and occurrences of specific letters, as well as creating and modifying binary files. The exercises are designed to enhance understanding of file operations in Python.

Uploaded by

gbbloodshot41
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

EXERCISE OF FILE HANDLING

1. Write a function in python to read the content from a text file "poem.txt" line
by line and display the same on screen.
2. Write a function in python to count the number of lines from a text file
"story.txt" which is not starting with an alphabet "T".

Example: If the file "story.txt" contains the following lines:

“A boy is playing there.


There is a playground.
An aeroplane is in the sky.
The sky is pink.
Alphabets and numbers are allowed in the password.”

The function should display the output as 3

3. Write a function in Python to count and display the total number of words in a
text file.
4. Write a function in Python to read lines from a text file "notes.txt". Your
function should find and display the occurrence of the word "the".

For example: If the content of the file is:


"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."

The output should be 5

5. Write a function display_words() in python to read lines from a text file


"story.txt", and display those words, which are less than 4 characters.
6. Write a function in Python to count the words "this" and "these" present in a
text file "article.txt". [Note that the words "this" and "these" are complete
words].
7. Write a function in Python to count words in a text file those are ending with
alphabet "e".
8. Write a function in Python to count uppercase character in a text file.
9. 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 hash_display() 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 hash_display() should display the following content :
T#H#E# #W#O#R#L#D# #I#S# #R#O#U#N#D#

10. 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 otherwise.

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

11. Write a function AMCount() in Python, which should read each


character of a text file STORY.TXT, should count and display the
occurance of alphabets A and M (including small cases a and m
too).
For Example:
“If the file content is as follows:
Updated information
As simplified by official websites.”
The EUCount() function should display the output as:
A or a:4
M or m :2
12. A binary file "Book.dat" has structure [BookNo, Book_Name,
Author, Price].
i. Write a user defined function createFile() to input data for a
record and add to Book.dat.
ii. Write a function countRec(Author) in Python which accepts the
Author name as parameter and count and return number of books
by the given Author are stored in the binary file "Book.dat".
13. A binary file "STUDENT.DAT" has structure (admission_number,
Name, Percentage). Write a function count_rec() in Python that
would read contents of the file "STUDENT.DAT" and display the
details of those students whose percentage is above 75. Also
display number of students scoring above 75%.

14. Given a binary file employee.dat, created using dictionary


object having keys: (empcode, name, and salary)

a) Write a python function that adds one more record at the end of file.
b) Write a python function that displays all employee records whose salary
is more than 30000.

15. A binary file players.dat, containing records of following list format:


[code, name, country and total runs].

a) Write a python function that display all records where player name starts
from 'A'
b) Write a python function that accept country as an argument and count and
display the number of players of that country.
c) Write a python function that adds one record at the end of file.

16. Given a binary file game.dat, containing records of following


list format: [game_name, participants]

a) Write a function in Python that would read contents from the file game.dat
and creates a file named basket.dat copying only those records from
game.dat where the game name is "Basket Ball"

You might also like