SlideShare a Scribd company logo
1 of 26
Download to read offline
Binary file operations
• All binary files follow a specific format. We can open some binary files in the normal text
editor but we can’t read the content present inside the file. That’s because all the binary
files will be encoded in the binary format, which can be understood only by a computer
or a machine.
• 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
• If we want to write a structure such as list or dictionary to a file and read it
subsequently, we need to use the Python module pickle.
• Pickling is the process of converting structure to a byte stream before writing to a
file
• While reading the content of file, a reverse process called Unpickling is used to
convert the byte stream back to the original format.
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
As shown in the above program, inserting and adding
record in student file begins with importing pickle module.
Then, iterating for the number of records to be added is
done using while loop. Input is accepted from the user for
roll number, name and marks. These fetched values are
saved as a list to be appended. Once the record is created,
it is appended to the binary file “student” using the dump()
method, which writes the object into the opened file.
Reading a record from a binary file
Maninder Singh 10
Reading a record from a binary file
Maninder Singh 11
• The above program deals with reading the contents from binary file
student using load() method of pickle module. It is used to read the
object from the opened file. The syntax for this is given by the
statement—
object = pickle.load(file)
• Once the contents are read, it gets stored inside the object, rec. All
the data of the respective fields from this object is held in the
respective variables, roll, name and marks and are finally displayed as
the output.
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
• First we need to search the record from the binary file, which is to be updated.
• Once the record is found, the file pointer is moved to the beginning of the file
using seek(0) statement, and then the changed name is written to the file and the
record is updated.
• seek() method is used for random access to the file.
Maninder Singh 13
Updating a record in the file requires roll number to be
fetched from the user whose name is to be updated
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
• Python allow random access of the data as well using built-in methods seek() and tell().
• 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) #where f is the file pointer
• 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

More Related Content

Similar to 5.1 Binary File Handling.pdf

File handing in C
File handing in CFile handing in C
File handing in Cshrishcg
 
Binary File.pptx
Binary File.pptxBinary File.pptx
Binary File.pptxMasterDarsh
 
FIle Handling and dictionaries.pptx
FIle Handling and dictionaries.pptxFIle Handling and dictionaries.pptx
FIle Handling and dictionaries.pptxAshwini Raut
 
An Introduction To Python - Modules & Solving Real World Problems
An Introduction To Python - Modules & Solving Real World ProblemsAn Introduction To Python - Modules & Solving Real World Problems
An Introduction To Python - Modules & Solving Real World ProblemsBlue Elephant Consulting
 
File Handling In C++(OOPs))
File Handling In C++(OOPs))File Handling In C++(OOPs))
File Handling In C++(OOPs))Papu Kumar
 
Linker and loader upload
Linker and loader   uploadLinker and loader   upload
Linker and loader uploadBin Yang
 
Yihan Lian & Zhibin Hu - Smarter Peach: Add Eyes to Peach Fuzzer [rooted2017]
Yihan Lian &  Zhibin Hu - Smarter Peach: Add Eyes to Peach Fuzzer [rooted2017]Yihan Lian &  Zhibin Hu - Smarter Peach: Add Eyes to Peach Fuzzer [rooted2017]
Yihan Lian & Zhibin Hu - Smarter Peach: Add Eyes to Peach Fuzzer [rooted2017]RootedCON
 
Build process ppt.pptx
Build process ppt.pptxBuild process ppt.pptx
Build process ppt.pptxSHIVANISRECECE
 
File management
File managementFile management
File managementsumathiv9
 
1 cs xii_python_file_handling text n binary file
1 cs xii_python_file_handling text n binary file1 cs xii_python_file_handling text n binary file
1 cs xii_python_file_handling text n binary fileSanjayKumarMahto1
 
Pf cs102 programming-8 [file handling] (1)
Pf cs102 programming-8 [file handling] (1)Pf cs102 programming-8 [file handling] (1)
Pf cs102 programming-8 [file handling] (1)Abdullah khawar
 

Similar to 5.1 Binary File Handling.pdf (20)

637225560972186380.pdf
637225560972186380.pdf637225560972186380.pdf
637225560972186380.pdf
 
File handing in C
File handing in CFile handing in C
File handing in C
 
C- language Lecture 8
C- language Lecture 8C- language Lecture 8
C- language Lecture 8
 
Binary File.pptx
Binary File.pptxBinary File.pptx
Binary File.pptx
 
FIle Handling and dictionaries.pptx
FIle Handling and dictionaries.pptxFIle Handling and dictionaries.pptx
FIle Handling and dictionaries.pptx
 
Python file handling
Python file handlingPython file handling
Python file handling
 
Chapter - 5.pptx
Chapter - 5.pptxChapter - 5.pptx
Chapter - 5.pptx
 
PHP Function
PHP Function PHP Function
PHP Function
 
File Handling In C++
File Handling In C++File Handling In C++
File Handling In C++
 
An Introduction To Python - Modules & Solving Real World Problems
An Introduction To Python - Modules & Solving Real World ProblemsAn Introduction To Python - Modules & Solving Real World Problems
An Introduction To Python - Modules & Solving Real World Problems
 
File Handling In C++(OOPs))
File Handling In C++(OOPs))File Handling In C++(OOPs))
File Handling In C++(OOPs))
 
Filepointers1 1215104829397318-9
Filepointers1 1215104829397318-9Filepointers1 1215104829397318-9
Filepointers1 1215104829397318-9
 
Linker and loader upload
Linker and loader   uploadLinker and loader   upload
Linker and loader upload
 
Data file handling
Data file handlingData file handling
Data file handling
 
Yihan Lian & Zhibin Hu - Smarter Peach: Add Eyes to Peach Fuzzer [rooted2017]
Yihan Lian &  Zhibin Hu - Smarter Peach: Add Eyes to Peach Fuzzer [rooted2017]Yihan Lian &  Zhibin Hu - Smarter Peach: Add Eyes to Peach Fuzzer [rooted2017]
Yihan Lian & Zhibin Hu - Smarter Peach: Add Eyes to Peach Fuzzer [rooted2017]
 
Build process ppt.pptx
Build process ppt.pptxBuild process ppt.pptx
Build process ppt.pptx
 
File management
File managementFile management
File management
 
Python-FileHandling.pptx
Python-FileHandling.pptxPython-FileHandling.pptx
Python-FileHandling.pptx
 
1 cs xii_python_file_handling text n binary file
1 cs xii_python_file_handling text n binary file1 cs xii_python_file_handling text n binary file
1 cs xii_python_file_handling text n binary file
 
Pf cs102 programming-8 [file handling] (1)
Pf cs102 programming-8 [file handling] (1)Pf cs102 programming-8 [file handling] (1)
Pf cs102 programming-8 [file handling] (1)
 

Recently uploaded

Play hard learn harder: The Serious Business of Play
Play hard learn harder:  The Serious Business of PlayPlay hard learn harder:  The Serious Business of Play
Play hard learn harder: The Serious Business of PlayPooky Knightsmith
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxJisc
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17Celine George
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxPooja Bhuva
 
Tatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsTatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsNbelano25
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024Elizabeth Walsh
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxPooja Bhuva
 
What is 3 Way Matching Process in Odoo 17.pptx
What is 3 Way Matching Process in Odoo 17.pptxWhat is 3 Way Matching Process in Odoo 17.pptx
What is 3 Way Matching Process in Odoo 17.pptxCeline George
 
How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17Celine George
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Pooja Bhuva
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptNishitharanjan Rout
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
 
21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptxJoelynRubio1
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...Amil baba
 

Recently uploaded (20)

Play hard learn harder: The Serious Business of Play
Play hard learn harder:  The Serious Business of PlayPlay hard learn harder:  The Serious Business of Play
Play hard learn harder: The Serious Business of Play
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
Tatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsTatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf arts
 
VAMOS CUIDAR DO NOSSO PLANETA! .
VAMOS CUIDAR DO NOSSO PLANETA!                    .VAMOS CUIDAR DO NOSSO PLANETA!                    .
VAMOS CUIDAR DO NOSSO PLANETA! .
 
OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
What is 3 Way Matching Process in Odoo 17.pptx
What is 3 Way Matching Process in Odoo 17.pptxWhat is 3 Way Matching Process in Odoo 17.pptx
What is 3 Way Matching Process in Odoo 17.pptx
 
Our Environment Class 10 Science Notes pdf
Our Environment Class 10 Science Notes pdfOur Environment Class 10 Science Notes pdf
Our Environment Class 10 Science Notes pdf
 
How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.ppt
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 

5.1 Binary File Handling.pdf

  • 1. Binary file operations • All binary files follow a specific format. We can open some binary files in the normal text editor but we can’t read the content present inside the file. That’s because all the binary files will be encoded in the binary format, which can be understood only by a computer or a machine. • 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
  • 2. Binary file operations • If we want to write a structure such as list or dictionary to a file and read it subsequently, we need to use the Python module pickle. • Pickling is the process of converting structure to a byte stream before writing to a file • While reading the content of file, a reverse process called Unpickling is used to convert the byte stream back to the original format. Maninder Singh 2
  • 3. 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
  • 4. Program to write structure, list in the binary file Maninder Singh 4
  • 5. Program to write structure, dictionary in the binary file Maninder Singh 5
  • 6. Program to read list items from the binary file Maninder Singh 6
  • 7. Program to read dictionary items from the binary file Maninder Singh 7
  • 8. Insert/Append a Record in Binary File Maninder Singh 8
  • 9. Maninder Singh 9 As shown in the above program, inserting and adding record in student file begins with importing pickle module. Then, iterating for the number of records to be added is done using while loop. Input is accepted from the user for roll number, name and marks. These fetched values are saved as a list to be appended. Once the record is created, it is appended to the binary file “student” using the dump() method, which writes the object into the opened file.
  • 10. Reading a record from a binary file Maninder Singh 10
  • 11. Reading a record from a binary file Maninder Singh 11 • The above program deals with reading the contents from binary file student using load() method of pickle module. It is used to read the object from the opened file. The syntax for this is given by the statement— object = pickle.load(file) • Once the contents are read, it gets stored inside the object, rec. All the data of the respective fields from this object is held in the respective variables, roll, name and marks and are finally displayed as the output.
  • 12. Program to search a record from the binary file "student.dat" on the basis of roll number. Maninder Singh 12
  • 13. Updating a record in a binary file • First we need to search the record from the binary file, which is to be updated. • Once the record is found, the file pointer is moved to the beginning of the file using seek(0) statement, and then the changed name is written to the file and the record is updated. • seek() method is used for random access to the file. Maninder Singh 13
  • 14. Updating a record in the file requires roll number to be fetched from the user whose name is to be updated Maninder Singh 14
  • 15. Display Record (Formatted Output) Maninder Singh 15
  • 16. Display Record (Formatted Output) Maninder Singh 16
  • 17. Searching in Binary File (Search Form) Maninder Singh 17
  • 18. Finding Number of Record in Binary File Maninder Singh 18
  • 19. Deleting a record in a binary file Maninder Singh 19 Existing Records in File
  • 20. Deleting a record in a binary file Maninder Singh 20
  • 21. RANDOM ACCESS IN FILES • Python allow random access of the data as well using built-in methods seek() and tell(). • 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
  • 22. ABSOLUTE POSITIONING • The syntax for seek() is— • f.seek(file_location) #where f is the file pointer • 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
  • 23. 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
  • 24. 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
  • 25. 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