Text file and Binary file work sheet
Text file and Binary file work sheet
1. Write a function in Python to read a text file, Alpha.txt and displays those lines which
begin with the word ‘You’.
2. Write a function, vowelCount() in Python that counts and displays the number of vowels
in the text file named Poem.txt.
3. Write a Python function that displays all the words containing @cmail from a text file
"Emails.txt".
4. Write a Python function that finds and displays all the words longer than 5 characters
from a text file "Words.txt".
5. 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’.
6. Write a user defined function countwords() to display the total number of words
present in the file from a text file “Quotes.Txt”.
7. Write a user defined function countwords() to display the total number of words
present in the file from a text file “Quotes.Txt”.
8. Write a function COUNT_AND( ) in Python to read the text file “STORY.TXT” and count
the number of times “AND” occurs in the file. (include AND/and/And in the counting)
9. Write a function DISPLAYWORDS( ) in python to display the count of words starting with
“t” or “T” in a text file ‘STORY.TXT’.
10.Write a function to display those lines which start with the letter “G” from the text file
“MyNotes.txt”
11.Write a function in python to read lines from file “POEM.txt” and display all
12.Write a function COUNT() in Python to read contents from file “REPEATED.TXT”, to
count and display the occurrence of the word “Catholic” or “mother”.
13.Write a method/function COUNTLINES_ET() in python to read lines from a text file
REPORT.TXT, and COUNT those lines which are starting either with ‘E’ and starting with
‘T’ respectively. And display the Total count separately.
14.Write a method/function SHOW_TODO() in python to read contents from a text file
ABC.TXT and display those lines which have occurrence of the word ‘‘TO’’ or ‘‘DO’’.
15.Write a method/function ISTOUPCOUNT() in python to read contents from a text file
WRITER.TXT, to count and display the occurrence of the word ‘‘IS’’ or ‘‘TO’’ or ‘‘UP’’
16.Write a function VowelCount() in Python, which should read each character of a text file
MY_TEXT_FILE.TXT, should count and display the occurrence of alphabets vowels.
17.Write a function filter(oldfile, newfile) that copies all the lines of a text file “source.txt”
onto “target.txt” except those lines which starts with “@” sign.
Binary File
1. A binary file “student.dat” has structure [rollno, name, marks].
i. Write a user defined function insertRec() to input data for a student and add to
student.dat.
ii. Write a function searchRollNo( r) in Python which accepts the student’s rollno as
parameter and searches the record in the file “student.dat” and shows the details of
student i.e. rollno, name and marks (if found) otherwise shows the message as ‘No
record found’.
2. A binary file “emp.dat” has structure [EID, Ename, designation, salary].
i. Write a user defined function CreateEmp() to input data for a record and create a
file emp.dat.
ii. Write a function display() in Python to display the detail of all employees whose
salary is more than 50000.
3. Write a python program to append a new records in a binary file –“student.dat”. The
record can have Rollno, Name and Marks.
4. A binary file named “EMP.dat” has some records of the structure [EmpNo, EName, Post,
Salary] (a) Create a binary file “EMP.dat” that stores the records of employees and
display them one by one.
(b) Display the records of all those employees who are getting salaries between 25000
to 30000.
5. A binary file “Book.dat” has structure {BookNo:[ Book_Name, Author, Price]}.
a) Write a user defined function CreateFile() to input data for a record and add to
“Book.dat” .
b) 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”
6. Consider a binary file emp.dat having records in the form of dictionary. E.g {eno:1,
name:”Rahul”, sal: 5000} write a python function to display the records of above file
for those employees who get salary between 25000 and 30000