[go: up one dir, main page]

0% found this document useful (0 votes)
33 views16 pages

Data File Cbse

Uploaded by

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

Data File Cbse

Uploaded by

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

UNIT -4 : DATA FILE HANDLING

• 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.

Why the Files are used?


• The data stored with in a file is known as persistent data because this data is
permanently stored in the system.
• Python provides reading and writing capability of data files.
• We save the data in the files for further use.
• As you save your data in files using word, excel etc. same thing we
• can do with python.
• “A File is a collection of characters in which we can perform readand write
functions. And also we can save it in secondary storage.”

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>)

File accessing modes


> read( r ) : To read a file
> write(w) : To write into a file
> append( a) : To write at the end of the file
Mode Description

r To read the file which is already existing.


rb Read Only in binary format.
r+ To Read and write but the file pointer will be at the beginning of the file.

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.

Opening a File Using the `with` Clause


In Python, file handling is a fundamental aspect of programming. You can work with files for
various purposes, such as reading data, writing data, and appending to existing files. A
common practice when working with files is to use the `with` clause, which simplifies file
handling by automatically managing resources and ensuring that the file is properly closed
when you are done with it.

Here's how to open a file using the `with` clause:


Syntax :
with open("filename.txt", "mode") as file:
# Code to work with the file
- `"filename.txt"` is the name of the file you want to open.
- `"mode"` specifies the mode in which you want to open the file, such as `"r"` for reading,
`"w"` for writing, or `"a"` for appending.

Writing and Appending Data to a Text File


a) Writing Data to a File
To write data to a text file, you can use the `write()` method. It allows you to write a single
string to the file. If the file doesn't exist, it will be created. If it does exist, the previous content
will be overwritten.
Example Code :

30
with open("example.txt", "w") as file:
file.write("This is a line of text.\n")

b) Appending Data to a File


To append data to an existing text file, you can use the `write()` method in append mode
(`"a"`). This will add the new data to the end of the file without overwriting the existing
content.
Example Code :
with open("example.txt", "a") as file:
file.write("This is another line of text.\n")

Reading from a Text File


a) Reading the Entire File
To read the entire contents of a text file, you can use the `read()` method. It returns the file's
content as a string.
Code Example:
with open("example.txt", "r") as file:
content = file.read()
print(content)
b) Reading a Single Line
To read a single line from a text file, you can use the `readline()` method. It reads and returns
a single line from the file.
Example Code :
with open("example.txt", "r") as file:
line = file.readline()
print(line)
c) Reading All Lines
To read all lines of a text file into a list, you can use the `readlines()` method. Each line is
stored as an element in the list.
Example Code :
with open("example.txt", "r") as file:
lines = file.readlines()

31
for line in lines:
print(line)

Seek and Tell Methods


- The `seek()` method is used to move the file's cursor to a specified position within the file.
This is helpful when you want to read or write data from a specific location.
Example Code :
with open("example.txt", "r") as file:
file.seek(5) # Move to the 6th character
content = file.read()
print(content)
- The `tell()` method is used to determine the current position of the file's cursor. It returns the
current file pointer's position.
Example Code
with open("example.txt", "r") as file:
file.read(10) # Read the first 10 characters
position = file.tell()
print("Current position:", position)

Binary File Handling

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.

The tell() Function


The tell() method of python tells us the current position within the file.

The seek() Function


The seek(offset, from) method changes the current file position. 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

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()

The Pickle Module :

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.

Serialization is the process of transforming data or an object in memory (RAM) to a stream of


bytes called byte streams. These byte streams in a binary file can then be stored in a disk or in
a database or sent through a network. Serialization process is also called pickling.
De-serialization or unpickling is the inverse of pickling process where a byte stream is
converted back to Python object.

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.

The dump() method:

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).

Syntax of dump() is as follows:


dump(data_object, file_object)

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.

#Pickling data in Python


import pickle
listvalues=[1,"Geetika",'F', 26]
fi leobject=open("mybinary.dat", "wb")
pickle.dump(listvalues,fi leobject)
fileobject.close()

The load() method

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().

#Unpickling data in Python


import pickle
print("The data that were stored in file are: ")
fileobject=open("mybinary.dat","rb")
objectvar=pickle.load(fileobject)
fileobject.close()
print(objectvar)

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 b. Binary file

2. The command for opening a file in Python file handling is ____________.


a. open() b. update() c. both a) and b) d. None of the above

Answer a. open()

3. The command for closing a file in Python file handling is ____________.

a. close() b. closing() c. object() d. None of the above

Answer ⟵ a. close()

4. _________ text file mode is used to read data from file.

a. ‘r’ b. ‘rb’ c. ‘r+’ d. None of the above

Answer a. ‘r’

5. _________ text file mode is used to append data in the file using file handling.

a. ‘w’ b. ‘ab’ c. ‘a’ d. None of the above

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+

7. Which of the following is not true about binary files?

a) Binary files are store in terms of bytes


b) When you open binary file in text editor will show garbage values
c) Binary files represent ASCII value of characters
d) All of the above
Ans: c) Binary files represent ASCII value of characters

8. What is the difference between wb and wb+ mode?

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.

9. The pickle module in Python is used for:

a) Serializing any Python object structure b) De-serializing Python object structure


c) Both a and b d) None of these
Ans: c) Both a and b
10. Which method is used to convert Python objects for writing data in binary file?

a) write() b) load() c) store() d) dump()

Ans: d) dump()

11. seek() function is used for _______.

a) positions the file object at the specified location.

b) It returns the current position of the file object

c) It writes the data in binary file

d) None of these

Ans: a) positions the file object at the specified location.

12. Which is not the valid mode for binary files?

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?

a) read() b) open() c) dump() d) load()

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.

a) seek() b) load() c) position() d) tell()


Ans: d) tell()

(ASSERTION AND REASONING based questions)

Mark the correct choice as:

i. Both A and R are true and R is the correct explanation for A

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

2. Assertion(A): an open file can be close using close() function.

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.

Reason(R): Pickle.load() function is used to retrieve pickled data.

Ans: ii. Both A and R are true but R is not the correct explanation for A

4. Assertion(A): The seek(offset,from) method changes the current file position.

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.

Ans: iii. A is True but R is False

(TRUE/FALSE Questions)

1. Pickling is a process by which a Python object is converted to a byte stream.


Ans: True
2. The load() function of the pickle module performs pickling.
Ans: False
3. The dump() function of the pickle module performs unpickling.
Ans: False
4. When you open a file for writing, if the file does not exist, a new file is created.
Ans: True
5. When you open a file for appending, if the file exists, the existing file is overwritten with the new
file.

Ans: False

Short Answer Type Questions ( 2-Marks Questions )

1. Identify the error in the following code:


import pickle
data=[‘one’, 2, [3, 4, 5]]
with open(‘data2.dat’, ‘rb’) as f:
pickle.dump(data, f)
Ans: The file is opened in read mode and dump() function tries to write onto file, hence there
is error line 3.
Correct code : with open(‘data2.dat’, ‘wb’) as f:
2. Any recipe uses some ingredients. Write a program to store the list of ingredients in a
binary file recipe.dat .
Ans: import pickle
ingredients= [‘cucumber’, ‘pumpkin’, ‘carrot’, ‘peas’]
with open(‘recipe.dat’, ‘wb’) as fout:
pickle.dump(ingredient, fout)
3. A binary file “student.dat” has structure [rollno, name, marks]. Write a user defined
function insertRec() to input data for a student and add to student.dat.
Ans:
import pickle
def insertRec():
f=open(‘student.dat’,’ab’)
rollno = int (input(‘Enter Roll Number :’))
name=input("Enter Name :")
marks = int(input(‘Enter Marks :’))
rec = [rollno, name, marks ]
38
pickle.dump( rec, f )
f.close()
4. Considering the following definition of dictionary MULTIPLEX, write a method in
python to search and display all the content in a pickled file CINEMA.DAT, where
MTYPE key of the dictionary is matching with the value ‘Comedy’.
MULTIPLEX = {‘MNO’ : ____, ‘MNAME’:_____, ‘MTYPE’:______}
Ans:
import pickle
def search():
file=open('CINEMA.DAT', 'rb')
try:
while True:
MULTIPLEX=pickle.load(file)
if(MULTIPLEX['MTYPE']=='Comedy'):
print(MULTIPLEX)
except EOFError:
f.close()
5. What will be the output of following code:
import pickle
names=['First', 'Second', 'Third', 'Fourth', 'Fifth']
lst=[ ]
for i in range(-1, -5, -1):
lst.append(names[i])
fout= open('test.dat', 'wb')
pickle.dump(lst, fout)
fout.close()
fin= open('test.dat', 'rb')
nlist=pickle.load(fin)
fin.close()
print(nlist)
Ans: ['Fifth', 'Fourth', 'Third', 'Second']

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()

Case Study Based Question-1

Ramesh loves programming. He joined an institute for learning. He is learning python. He


learned all the python concepts like strings, lists, tuple, dictionaries etc. but he wants to learn
file handling in python. He is trying to learn binary file handling. His teacher gave him partial
code to write and read data from employee.dat having structure empno, name, salary. Help
Ramesh to complete the code:

___________________ # 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.

a) csv b) random c) pickle d) file

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)

c) pickle.load(dict,fr) d) none of these

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

(i). Help Abhay to complete the Statement1.


Ans: f = open('bank.dat','wb')
(ii). How will he write the record in Statement2?
Ans: rec = [acc_no, cust_name, bal]
(iii). What code will be there in Statement3 to write record in file?
Ans: pickle.dump(rec, f)
(iv). Complete the Statement4 to open the file with correct mode.
Ans: f = open('bank.dat','rb')
(v). What will be the Statement5?
Ans: rec = pickle.load(f)

43

You might also like