0% found this document useful (0 votes)
10 views4 pages

Binary Files

binary files
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)
10 views4 pages

Binary Files

binary files
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/ 4

Page 1 of 4

BINARY FILES
o Binary file: basic operations on a binary file:
o Open using file open modes (rb, rb+,wb,wb+, ab, ab+),
o Close a binary file,
o import pickle module, dump() and load() method,
o read, write/create, search, append and update operations in a binary file.

Binary files store data in the binary format (0’s and 1’s) which is understandable
by the machine. So when we open the binary file in our machine, it decodes the
data and displays in a human-readable format.
There are three basic modes of a binary file:

 read: This mode is written as rb


 write: This mode is written as wb
 append: This mode is written as ab

The plus symbol followed by file mode is used to perform multiple operations
together. For example, rb+ is used for reading the opening file for reading and writing.
The cursor position isat the beginning when + symbol is written with file mode.

To open a binary file follow this syntax:

file = open(<filepath>, mode)


For example: f = open(“one.dat”,”rb”)

Binary File Modes: File mode governs the type of operations read/write/append
possible in theopened file. It refers to how the file will be used once its opened.
File Description
Mode
rb Read Only: Opens existing file for read operation
wb Write Only: Opens file for write operation. If file does not exist, file is created. Iffile exists, it
overwrites data.
ab Append: Opens file in write mode. If file exist, data will be appended at the end.
rb+ Read and Write: File should exist, Both read and write operations can beperformed.

wb+ Write and Read: File created if not exist, If file exist, file is truncated.
ab+ Write and Read: File created if does not exist, If file exist data is truncated.

Pickle Module: Python pickle is used to serialize and deserialize a python object structure.
Any object on python can be pickled so that it can be saved on disk.

Pickling: Pickling is the process whereby a Python object hierarchy is


converted into a byte stream. It is also known as serialization
Page 2 of 4

Unpickling: A byte stream is converted into object hierarchy.


To use the pickling methods in a program, we have to import pickle
module using import keyword.

Example:
import pickle
In this module,we shall discuss two functions of pickle module, which are:
i) dump():To store/write the object data to the file.
ii) load():To read the object data from a file and returns the object data.

Syntax:
Write the object to the file:

pickle.dump(objname, file-object )

Read the object from a file:

pickle.load(file-object)
Page 3 of 4
Page 4 of 4

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy