File Handling
File Handling
1 Write a user defined function in Python that displays the number of lines starting with ‘H’ in the file Para.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.
2. Consider a binary file Employee.dat containing details such as empno:ename:salary (separator ‘ :’). Write a python
function to display details of those employees who are earning between 20000 and 40000.(both values inclusive)
3. 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
Write a user defined function to display the total number of words present in the file.
4. Write a user-defined function named Count() that will read the contents of text file named “Report.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.”
5. Write a function countmy( )in Python to read 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 preferences in the CHOICE section.”
The countmy( ) function should display the output as: “my occurs 2 times”.
6. Write a function in python to count the number of lines in a text file ‘STORY.TXT’ which is starting with an alphabet
‘A’ .
7. 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.
8. 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.
The output of the function should be: Count of Me/My in file: 4
9. 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).
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
10. 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”
11. A binary file “STUDENT.DAT” has structure (admission_number, Name, Percentage). Write a function countrec() 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%
12. Ranjan Kumar of class 12 is writing a program to create a CSV file “user.csv” which will contain user name and
password for some entries. He has written the following code. As a programmer, help him to successfully execute the
given task.
import _____________ # Line 1
addCsvFile(“Arjun”,”123@456”)
addCsvFile(“Arunima”,”aru@nima”)
addCsvFile(“Frieda”,”myname@FRD”)
readCsvFile() #Line 5
(a) Name the module he should import in Line 1.
(b) In which mode, Ranjan should open the file to add data into the file
(c) Fill in the blank in Line 3 to read the data from a csv file.
(d) Fill in the blank in Line 4 to close the file.
(e) Write the output he will obtain while executing Line 5.
13. Write a method COUNTLINES() in Python to read lines from text file ‘TESTFILE.TXT’ and display the lines which are
not starting with any vowel.
Example: If the file content is as follows:
An apple a day keeps the doctor away.
We all pray for everyone’s safety.
A marked difference will come in our country.
The COUNTLINES() function should display the output as: The number of lines not starting with any vowel - 1
14. Write a function ETCount() in Python, which should read each character of a text file “TESTFILE.TXT” and then
count and display the count of occurrence of alphabets E and T individually (including small cases e and t too).
Example: If the file content is as follows:
Today is a pleasant day.
It might rain today.
It is mentioned on weather sites
The ETCount() function should display the output as:
E or e: 6
T or t : 9
15. What is the advantage of using a csv file for permanent storage? Write a Program in Python that defines and calls
the following user defined functions:
(i) ADD() – To accept and add data of an employee to a CSV file ‘record.csv’. Each record consists of a list with field
elements as empid, name and mobile to store employee id, employee name and employee salary respectively.
(ii) COUNTR() – To count the number of records present in the CSV file named ‘record.csv’.
16. Give any one point of difference between a binary file and a csv file. Write a Program in Python that defines and
calls the following user defined functions:
(i) add() – To accept and add data of an employee to a CSV file ‘furdata.csv’. Each record consists of a list with field
elements as fid, fname and fprice to store furniture id, furniture name and furniture price respectively.
(ii) search()- To display the records of the furniture whose price is more than 10000.
17. Aman is a Python programmer. He has written a code and created a binary file record.dat with employeeid,
ename and salary. The file contains 10 records. He now has to update a record based on the employee id entered by
the user and update the salary. The updated record is then to be written in the file temp.dat. The records which are
not to be updated also have to be written to the file temp.dat. If the employee id is not found, an appropriate
message should to be displayed. As a Python expert, help him to complete the following code based on the
requirement given above: