5.1 Binary File Handling.pdf

keshavkumarsingh263 554 views 26 slides Jan 14, 2024
Slide 1
Slide 1 of 26
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26

About This Presentation

Binary file handling notes


Slide Content

Binary file operations
•Allbinaryfilesfollowaspecificformat.Wecanopensomebinaryfilesinthenormaltext
editorbutwecan’treadthecontentpresentinsidethefile.That’sbecauseallthebinary
fileswillbeencodedinthebinaryformat,whichcanbeunderstoodonlybyacomputer
oramachine.
•In binary files, there is no delimiter to end a line. Since they are directly in the form of
binary, hence there is no need to translate them. That’s why these files are easy and fast
in working.
Maninder Singh 1

Binary file operations
•Ifwewanttowriteastructuresuchaslistordictionarytoafileandreadit
subsequently,weneedtousethePythonmodulepickle.
•Picklingistheprocessofconvertingstructuretoabytestreambeforewritingtoa
file
•Whilereadingthecontentoffile,areverseprocesscalledUnpicklingisusedto
convertthebytestreambacktotheoriginalformat.
Maninder Singh 2

Steps to perform binary file operations
•First we need to import the module called pickle.
•This module provides 2 main functions:
•dump() : to write the object in file which is loaded in binary mode
•Syntax : dump(object_to_write, filehandle)
•load() : dumped data can be read from file using load() i.e. it is used to read
object from pickle file.
•Syntax:object = load(filehandle)
Maninder Singh 3

Program to write structure, list in the binary file
Maninder Singh 4

Program to write structure, dictionary in the binary file
Maninder Singh 5

Program to read list items from the binary file
Maninder Singh 6

Program to read dictionary items from the binary file
Maninder Singh 7

Insert/Append a Record in Binary File
Maninder Singh 8

Maninder Singh 9
Asshownintheaboveprogram,insertingandadding
recordinstudentfilebeginswithimportingpicklemodule.
Then,iteratingforthenumberofrecordstobeaddedis
doneusingwhileloop.Inputisacceptedfromtheuserfor
rollnumber,nameandmarks.Thesefetchedvaluesare
savedasalisttobeappended.Oncetherecordiscreated,
itisappendedtothebinaryfile“student”usingthedump()
method,whichwritestheobjectintotheopenedfile.

Reading a record from a binary file
Maninder Singh 10

Reading a record from a binary file
Maninder Singh 11
•Theaboveprogramdealswithreadingthecontentsfrombinaryfile
studentusingload()methodofpicklemodule.Itisusedtoreadthe
objectfromtheopenedfile.Thesyntaxforthisisgivenbythe
statement—
object=pickle.load(file)
•Oncethecontentsareread,itgetsstoredinsidetheobject,rec.All
thedataoftherespectivefieldsfromthisobjectisheldinthe
respectivevariables,roll,nameandmarksandarefinallydisplayedas
theoutput.

Program to search a record from the binary file
"student.dat" on the basis of roll number.
Maninder Singh 12

Updating a record in a binary file
•Firstweneedtosearchtherecordfromthebinaryfile,whichistobeupdated.
•Oncetherecordisfound,thefilepointerismovedtothebeginningofthefile
usingseek(0)statement,andthenthechangednameiswrittentothefileandthe
recordisupdated.
•seek()methodisusedforrandomaccesstothefile.
Maninder Singh 13

Updating a record in the file requires roll number to be
fetched from the user whose name is to beupdated
Maninder Singh 14

Display Record (Formatted Output)
Maninder Singh 15

Display Record (Formatted Output)
Maninder Singh 16

Searching in Binary File(Search Form)
Maninder Singh 17

Finding Number of Record in Binary File
Maninder Singh 18

Deleting a record in a binary file
Maninder Singh 19
Existing Records in File

Deleting a record in a binary file
Maninder Singh 20

RANDOM ACCESS IN FILES
•Pythonallowrandomaccessofthedataaswellusingbuilt-inmethodsseek()andtell().
•seek()—seek() function is used to change the position of the file handle (file pointer) to
a given specific position.
seek() can be done in two ways:
• Absolute Positioning
• Relative Positioning
Maninder Singh 21

ABSOLUTE POSITIONING
•The syntax for seek() is—
•f.seek(file_location)#wherefisthefilepointer
•For example
•f.seek(20)
•This statement shall move the file pointer to 20th byte in the file no matter where
you are.
Maninder Singh 22

RELATIVE POSITIONING
•Relative referencing/positioning has two arguments, offset and the position from
which it has to traverse.
•The syntax for relative referencing is: f.seek(offset, from_what)
•The "from_what" argument can have any of the three values:
•0: sets the reference point at the beginning of the file, which is by default.
•1: sets the reference point at the current file position.
•2: sets the reference point at the end of the file
Maninder Singh 23

RELATIVE POSITIONING
•For example,
•f.seek(–10,1) from current position, move 10 bytes backward
•f.seek(10,1) from current position, move 10 bytes forward
•f.seek(–20,1) from current position, move 20 bytes backward
•f.seek(10,0) from beginning of file, move 10 bytes forward
Maninder Singh 24

RANDOM ACCESS IN FILES
•tell()—tell() returns the current position of the file read/write pointer within the
file.
•Its syntax is:
f.tell() #where f is file pointer
•When you open a file in reading/writing mode, the file pointer rests at 0th byte.
•When you open a file in append mode, the file pointer rests at last byte.
Maninder Singh 25

Maninder Singh 26