0% found this document useful (0 votes)
22 views10 pages

Worksheet Filehandling 12.12.2024

The document is a worksheet for Velammal Bodhi Campus Thanjavur focused on data file handling in Python. It includes questions and tasks related to text and binary file operations, such as reading, writing, and manipulating files, as well as working with CSV files. The exercises require writing Python code to perform various file handling tasks and understanding file modes and functions.

Uploaded by

joyalrufus
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views10 pages

Worksheet Filehandling 12.12.2024

The document is a worksheet for Velammal Bodhi Campus Thanjavur focused on data file handling in Python. It includes questions and tasks related to text and binary file operations, such as reading, writing, and manipulating files, as well as working with CSV files. The exercises require writing Python code to perform various file handling tasks and understanding file modes and functions.

Uploaded by

joyalrufus
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

VELAMMAL BODHI CAMPUS THANJAVUR WORKSHEET 03

DATA FILE HANDLING


1 Give one difference between Text file and Binary File
Ans

2 Write a Python statement to open a text file “DATA.TXT” so that new contents can
be written on it.
Ans
3 Write a Python statement to open a text file “DATA.TXT” so that new content can be
added to the end of file
Ans
4 Write a Python statement to open a text file “DATA.TXT” so that existing contents
can be read from file.
Ans
5 A file “MYDATA.TXT” is opened as
file1 = open(“MYDATA.TXT”)
Write a Python statement to close this file.
Ans
6 What is the different in file opening mode “a” and “w” ?
Ans

6 What is the significance of adding „+‟ with file opening mode, with context to „r+‟ ?
Ans

7 What is the difference between readline() and readlines() ?


Ans

8 What is the purpose of using flush() in file handling operations ?


Ans

9 What is the advantage of opening file using “with‟ keyword?


Ans

10 Considering the content stored in file “TWINKLE.TXT”


Twinkle Twinkle, Little Star
How I wonder what you are
Up above the world so high
Like a diamond in the sky
Twinkle Twinkle Little Star
How I wonder what you are!
Write the output of following statements –
f = open("TWINKLE.TXT")
sr1 = # to read first line of file
str2 = # to read next line of file
str3 = # to read remaining lines of file

Ans

11 Considering the content stored in file “TWINKLE.TXT”


Twinkle Twinkle, Little Star
How I wonder what you are
Up above the world so high
Like a diamond in the sky
Twinkle Twinkle Little Star
How I wonder what you are!
Complete the missing statement using „for‟ loop to print all the lines of file
f = open(“TWINKLE.TXT”)
for :
print( )
Ans

12 What is the difference in write() and writelines()?


Ans

13 Considering the content stored in file “WORLDCUP.TXT”, write the output


India won the Cricket world cup of 1983
f = open(“WORLDCUP.TXT”)
print(f.read(2))
print(f.read(2))
print(f.read(4))
Ans

14 Write a function in python to count the number of lines in “POEM.txt” begins from
Upper case character. For e.g if the content of file is :
Twinkle Twinkle, Little Star
How I wonder what you are
up above the world so high
like a diamond in the sky
Twinkle Twinkle Little Star
How I wonder what you are!
Ans
15 Write a function in python to read lines from file “POEM.txt” and count how many
times the word “Twinkle ” exists in file.
For e.g. if the content of file is :
Twinkle Twinkle, Little Star
How I wonder what you are
Up above the world so high
Like a diamond in the sky
Twinkle Twinkle Little Star
How I wonder what you are!
Output should be: Number of time word Twinkle occurs : 4

Ans

16 Write a function in python to read lines from file “POEM.txt” and display all those
words, which has two characters in it.
For e.g. if the content of file is
Twinkle Twinkle, Little Star
How I wonder what you are
Up above the world so high
Like a diamond in the sky
Twinkle Twinkle Little Star
How I wonder what you are!

Ans

17 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”.
For example:
If the content of the file is “Nory was a Catholic because her mother was a Catholic ,
and Nory‟s mother was a Catholic because her father was a Catholic , and her father
was a
Catholic because his mother was a Catholic , or had been
The function should display:
Count of Catholic, mother is 9
Ans
18 Write a function dispS() in Python to read from text file “POEM.TXT” and display
Ans those lines which starts with “S”

19 Write a function COUNTSIZE() in Python to read the file “POEM.TXT” and display
size of file. For e.g. if the content of file is :
Twinkle Twinkle, Little Star
How I wonder what you are
Up above the world so high
Like a diamond in the sky

Ans

20 Write a python function ATOEDISP() for each requirement in Python to read the file
“NEWS.TXT” and
(I) Display “E” in place of all the occurrence of “A” in the word COMPUTER.
(II) Display “E” in place of all the occurrence of “A”:
I SELL COMPUTARS. I HAVE A COMPUTAR. I NEED A COMPUTAR. I WANT A
COMPUTAR. I USE THAT COMPUTAR. MY COMPUTAR CRASHED.
The function should display

(I) I SELL COMPUTERS. I HAVE A COMPUTER. I NEED A COMPUTER. I WANT A


COMPUTER. I USE THAT COMPTUER. MY COMPUTER CRASHED.
(II) I SALL COMPUTERS. I HEVE E COMPUTER. I NEED E COMPUTER. I WENT E
COMPUTER. I USE THET COMPTUER. MY COMPUTER CRESHED.
Ans

BINARY FILE HANDLING & CSV


1 Letter is prefixed to store string in binary form
Ans
2 Write a Python statement to open a text file “DATA.TXT” in binary mode so that
new contents can be written on it.
Ans
3 Write a Python statement to open a text file “DATA.TXT” in binary mode so that
new content can be added to the end of file
Ans
4 Write a Python statement to open a text file “DATA.TXT” in binary mode so that
existing contents can be read from file.
Ans
5 function is used to convert string in binary form.
Ans
6 Consider the following Python code, and fill in the blank to complete the
program
f=open("India.txt","wb")
str="India is my country"
f. (str.encode()) # statement to store the str in file
f.close()

Ans
7 function is used to fetch binary data from binary file
Ans
8 function is used to convert binary string to string
Ans
9 function is used in binary mode to send the read pointer to desired
position
Ans

Note: seek() function of file object is used to reposition the cursor


Syntax : seek(number of bytes to read, seek direction)
Seek direction can be 0 – beginning, 1 – current position , 2- from last (can be in
negative also for backward traversing)

10 Consider a binary file which stores Name of employee, where each name
occupies 20 bytes (length of each name) in file irrespective of actual
characters. Now you have to write code to access the first name, 5th name
and last name.

f = open("Emp.txt","rb")
s= #code to get first record
print(s.decode())
# code to position at 5th record
s = f.read(size)
print(s.decode())
# code to position at last record
s = f.read(20)
print(s.decode())
f.close()
Ans
11 Write a Python statement to reposition the read pointer to 20 bytes back
from the current position.
f = open("Emp.txt","rb")
f.read(20)
f.read(20)
f.read(20)
f. # reposition read pointer to previous record
f.close()
Ans
12 Write a function RECCOUNT() to read the content of binary file „NAMES.DAT‟
and display number of records ( each name occupies 20 bytes in file ) in it.
For. e.g. if the content of file is:
SACHIN
AMIT
AMAN
SUSHIL
DEEPAK
HARI SHANKER
Function should display
Total Records are 6
Ans

13 Write a function SCOUNT() to read the content of binary file „NAMES.DAT‟ and
display number of records (each name occupies 20 bytes in file ) where name
begins from „S‟ in it.
For. e.g. if the content of file is:
SACHIN
AMIT
AMAN
SUSHIL
DEEPAK
HARI SHANKER
Function should display
Total Names beginning from „S‟ are 2

Ans

14 To read and write collections like LIST, DICTIONARIES Python provides a module
called
Ans
15 is the process of converting structures to byte stream before writing to
file.
Ans
16 is the process of converting byte stream to original structure.
Ans
17 Pickling is done by the function
Ans
18 Unpickling is done by the function
Ans load()
19 Consider the following Python code and complete the missing statement:
import pickle
myfile = open("test.dat","wb")
d={1:100,2:200,3:300}
#statement to store dictionary d in file
myfile.close()
Ans
20 Consider the following Python code and complete the missing statement:
import pickle
myfile = open("test.dat","rb")
d= #statement to load dictionary data from file to „d‟
print(d)
myfile.close()
Ans
21 Python‟s standard streams are , ,
Ans
22 Python‟s standard streams are available in module
Ans
23 From the given path identify the type of each:
(i) C:\mydata\web\resources\img.jpg
(ii) ..\web\data.conf
Ans

24 Consider the following Binary file „Emp.txt‟, Write a function RECSHOW() to


display only those records who are earning more than 7000
Ans

24 CSV stands for


Ans
25 object is used to read data from csv file?
Ans
26 object is used to perform write operation on csv file.
Ans
27 function of writer object is used to send data to csv file to store.
Ans
28 Consider the following CSV file (emp.csv):
1,Peter,3500
2,Scott,4000
3,Harry,5000
4,Michael,2500
5,Sam,4200

Write Python function DISPEMP() to read the content of file emp.csv and display
only those records where salary is 4000 or above

Ans
29 Consider the following CSV file (emp.csv):
1,Peter,3500
2,Scott,4000
3,Harry,5000
4,Michael,2500
5,Sam,4200

Write a Python function DISPEMP() to read the content of file emp.csv and count
how many employee are earning less than 5000
Ans

30 Consider the following CSV file (emp.csv):


1,Peter,3500
2,Scott,4000
3,Harry,5000
4,Michael,2500
5,Sam,4200

Write a Python function SNAMES() to read the content of file emp.csv and display
the employee record whose name begins from „S‟ also show no. of employee with
first letter „S‟ out of total record.
Output should be:
2,Scott,4000
5,Sam,4200
Number of „S‟ names are 2/5
Ans

31 Write a python function CSVCOPY() to take sourcefile, targetfile as parameter


and create a targetfile and copy the contents of sourcefile to targetfile
Ans

32. Write a Python function that displays all the words starting from the letter ‘C’ in the text file "chars.txt".
33. Write a Python function that can read a text file and print only numbers stored in the file on the screen
(consider the text file name as "info.txt").
34. A csv file "candidates.csv" contains the data collected from an online application form for selection of
candidates for different posts, with the following data
 Candidate Name
 Qualification
 Percent_XII
 Percent_Qualification
E.g. [‘Smith Jones’, ‘M Tech’, 80, 76]
Write the following Python functions to perform the specified operations on this file:
a) READ() function which can read all the data from the file and display only records with Percent_XII more
than 75
b) IDENTIFY() function which can find and print the number of such records which are having Percent_XII
not more than 75
35. 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’.
36. 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.
37. A binary file named “EMP.dat” has some records of the structure [EmpNo, EName, Post, Salary]
(a) Write a user-defined function named NewEmp() to input the details of a new employee from the user and
store it in EMP.dat.
(b) Write a user-defined function named SumSalary(Post) that will accept an argument the post of employees &
read the contents of EMP.dat and calculate the SUM of salary of all employees of that Post.

You might also like