keshavkumarsingh263
554 views
26 slides
Jan 14, 2024
Slide 1 of 26
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
About This Presentation
Binary file handling notes
Size: 827.67 KB
Language: en
Added: Jan 14, 2024
Slides: 26 pages
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
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
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