Worksheet Filehandling 12.12.2024
Worksheet Filehandling 12.12.2024
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
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
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
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
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
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
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
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.