Data File Cbse
Data File Cbse
• We have seen yet only the transient programs. The programs which run for a short
period of time and give some output and after that their data is disappeared. And when
we again run those programs then we have to use new data.
• This is because the data is entered in primary memory which is temporary memory
and its data is volatile.
• Those programs which are persistent i.e. they are always in running or run for a long
time then their data is stored in permanent storage (e.g. harddisk) . If the program is
closed or restarted then the data used will be retrieved.
• For this purpose the program should have the capability to read or write the text files
or data files. These files can be saved in permanent storage.
• The meaning of File I/O (input-output) is to transfer the data from Primary memory
to secondary memory and vice-versa.
28
Basic File Types
Text File: A text file is sequence of line and line is the sequence of characters and this file is
saved in a permanent storage device. Although in python default character codingis ASCII
but by using constant ‘U’ this can be converted into UNICODE. In Text File each line
terminates with a special character which is EOL (End Of Line). These are in human readable
form and these can be created using any text editor.
Binary File: Binary files are used to store binary data suchas images, videos audio etc.
Generally numbers are stored in binary files. In binary file, there is no delimiter to end a line.
Since they are directly in the form of binary hence there is noneed to translate them. That’s
why these files are easy and fast in working.
Opening & Closing Files
➢ We need a file variable or file handle to work with files in Python.
➢ This file object can be created by using open( ) function or file( ) function.
➢ Open( ) function creates a file object, which is used later to access the file using the
functions related to file manipulation.
➢ Its syntax is following -
<file_object> = open(<file_name>,<access_mode>)
or
<file_object> = file(<file_name,<access_mode>)
rb+ To Read and write binary file. But the file pointer will be at the beginning ofthe file.
29
w Only writing mode, if file is existing the old file will be overwritten else the newfile will be
created.
wb Binary file only in writing mode, if file is existing the old file will be overwrittenelse the new
file will be created.
wb+ Binary file only in reading and writing mode, if file is existing the old file will be
overwritten else the new file will be created.
a Append mode. The file pointer will be at the end of the file.
ab Append mode in binary file. The file pointer will be at the end of the file.
a+ Appending and reading if the file is existing then file pointer will be at the endof the file else
new file will be created for reading and writing.
ab+ Appending and reading in binary file if the file is existing then file pointer willbe at the end of
the file else new file will be created for reading and writing.
30
with open("example.txt", "w") as file:
file.write("This is a line of text.\n")
31
for line in lines:
print(line)
Binary files are stored in terms of bytes (0s and 1s), but unlike text files, these bytes do not
represent the ASCII values of characters. Rather, they represent the actual content such as
image, audio, video, compressed versions of other files, executable files, etc. These files are
not human readable. Thus, trying to open a binary file using a text editor will show some
garbage values. We need specific software to read or write the contents of a binary file.
Binary files are stored in a computer in a sequence of bytes. Even a single bit change can
corrupt the file and make it unreadable to the supporting application. Also, it is difficult to
remove any error which may occur in the binary file as the stored contents are not human
readable. We can read and write both text and binary files through Python programs.
32
moved.
Example Code :
f = open("a.dat", 'wb')
line = ‘G20 Presidency\nOne Earth, One Family, One Future'
f.write(line)
f.close()
f = open("a.txt", 'rb+')
print(f.tell())
print(f.read(7)) # read seven characters
print(f.tell())
print(f.read())
print(f.tell())
f.seek(9,0) # moves to 9 position from beginning
print(f.read(5))
f.seek(4, 1) # moves to 4 position from current location
print(f.read(5))
f.seek(-5, 2) # Go to the 5th byte before the end
print(f.read(5))
f.close()
We know that Python considers everything as an object. So, all data types including list, tuple,
dictionary, etc. are also considered as objects. During execution of a program, we may require
to store current state of variables so that we can retrieve them later to its present
state. Suppose you are playing a video game, and after some time, you want to close it. So, the
program should be able to store the current state of the game, including current level/stage,
your score, etc. as a Python object. Likewise, you may like to store a Python dictionary as an
object, to be able to retrieve later. To save any object structure along with data, Python
provides a module called Pickle. The module Pickle is used for serializing
and de-serializing any Python object structure. Pickling is a method of preserving food items
by placing them in some solution, which increases the shelf life. In other words, it is a method
to store food items for later consumption.
The pickle module deals with binary files. Here, data are not written but dumped and
similarly, data are not read but loaded. The Pickle Module must be imported to load and dump
data. The pickle module provides two methods - dump() and load() to work with binary files
for pickling and unpickling, respectively.
33
This method is used to convert (pickling) Python objects for writing data in a binary file. The
file in which data are to be dumped, needs to be opened in binary write mode (wb).
where data_object is the object that has to be dumped to the file with the file handle named
file_object. For example, following Program writes the record of a student (roll_no, name,
gender and marks) in the binary file named mybinary.dat using the dump(). We need to close
the file after pickling.
This method is used to load (unpickling) data from a binary file. The file to be loaded is
opened in binary read (rb) mode. Syntax of load() is as follows:
Store_object = load(file_object)
Here, the pickled Python object is loaded from the file having a file handle named file_object
and is stored in a new file handle called store_object. The following program demonstrates
how to read data from the file mybinary.dat using the load().
Output of Program:
The data that were stored in fi le are:
[1, 'Geetika', 'F', 26]
Check Yourself :
MCQ:
1. _________file format are faster and easier for a prgram to read and write than other file
format.
34
a. Text file b. Binary file c. Doc file d. None of the above
Answer a. open()
Answer ⟵ a. close()
Answer a. ‘r’
5. _________ text file mode is used to append data in the file using file handling.
Answer c. ‘a’
6. Out of the followings which mode is used for both reading and writing in binary format in
file?
a) wb b) wb+ c) w d) w+
Ans: b) wb+
a) wb mode is used to open binary file in write mode and wb+ mode open binary file both for
read and write operation.
b) In wb mode file open in write mode and wb+ in read mode
35
c) File pointer is at beginning of file in wb mode and in wb+ at the end of file
d) No difference
Ans: a) wb mode is used to open binary file in write mode and wb+ mode open binary file
both for read and write operation.
Ans: d) dump()
d) None of these
a) r b) rb c) wb d) wb+
Ans: a) r
13. Which of the following function is used to read the data in binary file?
Ans: d) load()
14. Suresh wants to open the binary file student.dat in read mode. He writes the following
statement but he does not know the mode. Help him to find the same.
F=open(‘student.dat’, ____)
a) r b) rb c) w d) wb
36
Ans: b) rb
15. This method returns an integer that specifies the current position of the file object.
ii. Both A and R are true but R is not the correct explanation for A
iii. A is True but R is False
iv. A is false but R is True
1. Assertion (A): A binary file stores the data in the same way as stored in the memory.
Reason (R): Binary file in python does not have line delimiter
Ans: ii. Both A and R are true but R is not the correct explanation for A
Reason(R): sometimes the data written onto files is held in memory until the file is closed.
Ans: i. Both A and R are true and R is the correct explanation for A
3. Assertion(A): pickle.dump() function is used to store the object data to the file.
Ans: ii. Both A and R are true but R is not the correct explanation for A
Reason(R): If from is 0, the beginning of the file to seek. If it is set to 1, the current position
is used. If it is set to 2 then the end of the file would be taken as seek position. The offset
argument indicates the number of bytes to be moved.
Ans: i. Both A and R are true and R is the correct explanation for A
5. Assertion(A): ab+ mode is used for both appending and reading binary files and move file
pointer at end.
37
Reason(R): ab+ mode, if the file does not exist, it does not create a new file for reading and
writing.
(TRUE/FALSE Questions)
Ans: False
SA-2(3-Marks Questions)
1. A binary file “student.dat” has structure [rollno, name, marks]. 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’.
Ans:
def searchRollNo( r ):
f=open("student.dat","rb")
flag = False
while True:
try:
rec=pickle.load(f)
if rec[0] == r :
print(rec[‘Rollno’])
print(rec[‘Name’])
print(rec[‘Marks])
flag == True
39
except EOFError:
break
if flag == False:
print(“No record Found”)
f.close()
2. 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%.
Ans:
import pickle
def CountRec():
f=open("STUDENT.DAT","rb")
num = 0
try:
while True:
rec=pickle.load(f)
if rec[2] > 75:
print(rec[0], rec[1], rec[2])
num = num + 1
except:
f.close()
return num
3. A binary file named “EMP.dat” has some records of the structure [EmpNo, EName,
Post, Salary]. Create a binary file “EMP.dat” that stores the records of employees and
display them one by one. Also display the records of all those employees who are getting
salaries between 25000 to 30000.
Ans:
import pickle
f1 = open('emp.dat','rb')
try:
while True:
e = pickle.load(f1)
print(e)
except:
f1.close()
f1 = open('emp.dat','rb')
try:
while True:
e = pickle.load(f1)
if(e[3]>=25000 and e[3]<=30000):
print(e)
except:
f1.close()
4. A binary file “Book.dat” has structure [BookNo, Book_Name, Author, Price]. 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”.
Ans:
import pickle
40
def CountRec(Author):
f=open("Book.dat","rb")
num = 0
try:
while True:
rec=pickle.load(f)
if Author==rec[2]:
num = num + 1
except:
f.close()
return num
5. 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.
Ans:
import pickle
def search():
f=open(“emp.dat”,”rb”)
while True:
try:
d=pickle.load(f)
if(d[‘sal’]>=25000 and d[‘sal’]<=30000):
print(d)
except EOFError:
break
f.close()
___________________ # statement 1
def addrecords():
fw= _____________ #statement 2
dict={}
ch=’y’
while ch==’y’:
eno=int(input(“enter employee number”))
nm= input(“enter employee name”)
sal=int(input(“enter employee salary”))
dict={‘empno’:eno,’name’:nm,’salary’:sal}
____________________ # statement 3
ch=input(“add more record”)
fw.close()
# function to diplay records
def display():
41
dict={}
fr= _____________ # statement 4
dict=____________ # statement 5
fr.close()
print(“data :”,dict)
Answer questions (i)-(v) based on above case study
(i). Help Ramesh to import the module to perform binary file operation in statement 1.
Ans: c) pickle
(ii). Which statement is used from the following for statement 2 to open the binary file in
write mode?
a) open(“employee.dat”,’w’) b) open(“employee.dat”,’wb’)
c) open(“employee.dat”,’w+’) d) open(“employee.dat”,’r’)
Ans: b) open(“employee.dat”,’wb’)
(iii). Which statement is used from the following for statement 3 to write dictionary data
created in above code, namely dict, is written in binary file employee.dat file?
a) pickle.dump(dict,fw) b) pickle.write(dict,fw)
c) pickle.save(dict,fw) d) pickle.store(dict)
Ans: a) pickle.dump(dict,fw)
(iv). Which statement is used from the following for statement 4 to open the binary file in
read mode?
a) open(“employee.dat”,’r’) b) open(“employee.dat”,’r+’)
c) open(“employee.dat”,’a’) d) open(“employee.dat”,’rb’)
Ans: d) open(“employee.dat”,’rb’)
(v). Compelete statement 5 to read data in dictionary namely dict from the opened
binary file?
a) dict=pk.read(fr) b) dict=pickle.load(fr)
Ans: b) dict=pickle.load(fr)
42
Case Study Based Question-2
Abhay is a python learner. He created a binary file “Bank.dat” has structure as [account_no,
cust_name, balance]. He defined a function addfile( ) to add a record to Bank.dat. He also
defined a function CountRec( ) to count and return the number of customers whose balance
amount is more than 100000. He has some problem in the following codes.
import pickle
def addfile( ):
f = open('bank.dat', __________) #Statement1
acc_no = int(input('Enter account number: '))
cust_name = input('Enter name:')
bal = int(input('Enter balance'))
rec = ____________________ #Statement2
__________________ #Statement3
f.close()
def CountRec( ):
f = ________________ #Statement4
c=0
try:
while True:
rec = _______________ #Statement5
if rec[2] > 100000:
c += 1
except:
f.close()
return c
Answer questions (i)-(v) based on above case study
43