Data File Handling Worksheet
Data File Handling Worksheet
3. Your teacher has given you a method/function FilterWords() in python which read lines
from a text file NewsLetter. TXT, and display those words, which are lesser than 4
characters. Your teachers intentionally kept few blanks in between the code and asked you to
fill the blanks so that the code will run to find desired result. Do the needful with the
following python code. def FilterWords(): c=0 file=open(‘NewsLetter.TXT’,
‘_____’) #Statement-1
line = file._____ #Statement-2
word = _____ #Statement-3
for c in word:
if _____: #Statement-4
print(c)
_________ #Statement-5
__________ #Statement-6
a) Write the correct mode of opening the file in statement-1?
b) Fill in the blank in statement-2 to read the data from the file.
c) Fill in the blank in statement-3 to read data word by word.
d) Fill in the blank in statement-4, which display the word having lesser than 4 characters.
e) Fill in the blank in Statement-5 to close the file.
f) Fill in the blank in Statement-6 to call the function FilterWords().
4. Arun, during Practical Examination of Computer Science, has been assigned an incomplete
search () function to search a record which is passed as parameter in a pickled file student.dat.
The File student.dat is created by his Teacher and the following information is known about
the file.
(a) File contains details of students in [roll_no,name,marks] format.
(b) File contains details of 10 students (i.e. from roll_no 1 to 10) and separate list of each
student is written in the binary file using dump (). Arun has been assigned the task to
complete the code and print details of roll number1. def search(r):
f = open(“student.dat”,____) #Statement-1
____: #Statement-2
while True:
rec = pickle.____ #Statement-3
if(____): #Statement-4
________ #Statement-5 except:
pass
_____________ #Statement – 6
a) In which mode Arun should open the file in Statement-1?
b) Write the suitable code to be used at blank space in line marked as Statement-2.
c) Write the function (with argument), to be used at blank space in line marked as Statement-
3.
d) What will be the suitable code for blank space in line marked as Statement-4?
e) What will be the suitable code for blank space in line marked as Statement-5 to show record?
f) Which statement Arun should use at blank space in line marked as Statement-6 to close the
file?
2. 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
5. Write the definition of a function Count_Line() in Python, which should read each line
of a text file "SHIVAJI.TXT" and count total number of lines present in text file.
For example, if the content of the file "SHIVAJI.TXT" is as follows:
Shivaji was born in the family of Bhonsle.
He was devoted to his mother Jijabai.
India at that time was under Muslim rule.
The function should read the file content and display the output as follows: Total
number of lines: 3
6. Write a function RevString() prints the words to read a text file “Input.txt” and print
the words starting with ‘O’ in reverse order. The rest of the content is displayed
normally.
Example: If content in the text file is: UBUNTU IS AN OPEN SOURCE OPERATING
SYSTEM Output will be: UBUNTU IS AN NEPO SOURCE GNITAREPO SYSTEM
5. Consider a file FLIGHT.DAT containing multiple records. The structure of each record
is as shown below: [Fno, FName, Fare, Source, Destination].
Write a function COPY_REC () in Python that copies all those records from
FLIGHT.DAT where the source is DELHI and the destination is MUMBAI, into a new
file RECORD.DAT
6. A binary file data.dat needs to be created with following data written it in the form of
Dictionaries.
Write the following functions in python accommodate the data and manipulate it.
a) A function insert () that creates the data.dat file in your system and writes the
three records as dictionaries.
b) A function read() that reads the data from the binary file and displays the records
as dictionaries whose age is 16.
1. 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 ‘employee.csv’.
Each record consists of a list with field elements as eid, name and salary to store
employee id, employee name and employee salary respectively.
(ii) search ()- To display the records of the employee whose salary is more than
40000.
2. Rohan is making a software on "Countries & their Capitals" in which various records
are to be stored/retrieved in CAPITAL.CSV data file. It consists some records (Country
& Capital). Help him to define and call the following user defined functions:
(i) AddNewRec(Country, Capital) - To accept and add the records to a CSV file
"CAPITAL.CSV". Each record consists of a list with field elements as Country
and Capital to store country name and capital name respectively.
(ii) ShowRec-To display all the records present in the CSV file named
CAPITAL.CSV'. 3. Write a Program in Python that defines and calls the
following user defined functions:
3. Write a Program in Python that defines and calls the following user defined
functions:
(i) add() – To accept and add data of a to a CSV file ‘stud.csv’. Each record
consists of a list with field elements as admno, sname and per to store admission
number, student name and percentage marks respectively.
(ii) search()- To display the records of the students whose percentage is more than
75.
4. Write a Program in Python that defines and calls the following user defined functions:
(i) ADD() - To accept and add data of an item to a CSV file ‘events.csv'. Each
record consists of Event_id, Description, Venue, Guests, and Cost.
(ii) COUNTR() - To read the data from the file events.csv, calculate and display the
average number of guests and average cost.
5. Radha Shah is a programmer, who has recently been given a task to write a python code
to perform the following CSV file operations with the help of two user defined
functions/modules: Help him to define those two functions as per the following
instructions.
(a) CSVOpen() : to create a CSV file called “ books.csv” in append mode
containing information of books – Title, Author and Price.
(b) CSVRead() : to display the records from the CSV file called “ books.csv” .where
the field title starts with 'R'.
6. Write a Program in Python that defines and calls the following user defined functions:
(i) ADD_CONT() – To accept and add data of a contact to a CSV file ‘address.csv’.
Each record consists of a list with field elements as contId, cname and cmobile
to store contact id, contact name and contact number respectively.
(iii) COUNT_CONT() – To count the number of records present in the CSV file named
‘address.csv’
OUTPUT QUESTIONS
Absolute Path
An absolute path is a path that contains the entire path to the file or directory that you need to
access. Absolute paths ensure that Python can find the exact file on your computer. Absolute
file paths start with a leading forward slash and describe how to access a given file or directory,
starting from the root of the file system.
Relative Path
Generally ’.’ represents the absolute path of the folder where the py file is currently located
(parent folder), and ’..’ represents the absolute path of the upper level folder(parent of parent),
of the parent folder where the py file is currently located If you don’t add a dot, it means the
same layer as the py file, that is, the default. The term “parent” directory/folder is used to
describe the preceding folder in which a subfolder or file is created. A parent folder can have
many subfolders or files; thus, many subfolders can share the same parent folder.
Let’s take an example, a folder Test is created in C:\, Two subfolders Test1, Test2 and one
“x.txt” is created in Test Folder. “y.txt” and “Try.py” are created in folder Test1 and “z.txt” is
created in Test2