0% found this document useful (0 votes)
18 views

File Handling Binary File

Uploaded by

tomarchitransh3
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

File Handling Binary File

Uploaded by

tomarchitransh3
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 28

Binary File

Binary File
⚫ Binary files are used to store binary data such as
images, video,audio, documents, database etc.
⚫ In binary file, 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.
⚫ We can read Data of Binary files, only through Python
program.

⚫ If we open Binary file without python it display like


File Modes in Binary
Mode Description
rb Read Only in binary format.
Rb+ To Read and write binary file. But the file pointer will be at
the beginning of the file.

wb Binary file only in writing mode, if file is existing the old file
will be overwritten else 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.
ab Append mode in binary file. The file pointer will be at the
end of the file
Ab+ Appending and reading in binary file if the file is existing
then file pointer will be at the end of the file else new file will
be created for reading and writing
Opening File in Binary
⚫ File can be opened for read, write or append.
⚫ Syntax:
file_obj =open(file_name, access_mode)
or
file_obj = open(file_name)

* Default mode is “read”


Opening File
Note: assuming that “myfile,dat” is
already created file.

⚫f2=open('myfile.dat','rb')
(“myfile.dat” is relative file path)
Or
⚫f1=open("d:\\myfile.dat","rb")
(“d:\\myfile.dat” is absolute file path)
Closing File
⚫ f2=open('myfile.dat','rb')

⚫f2.close()
Note:
Open function is built in function, while
close() must be called through file
handler.
Writing data to binary file

encode() –It is function of string modue, It


converts string data to byte value (binary data)
Reading from binary file

Decode() - It converts byte value to string


data
Input/output screen

Input

Output of binary file


Pickle Module

⚫ To handle binary file operation, we use


special lib, pickle.

⚫ Installation of pickle lib use command-


- pip install pickle-mixin
Binary file operation
⚫ 1. dump()
Use to write different objects(like list, tuple, dictionary,
class) into file

Syntax - pickle.dump(obj, fileobj)

2. load()
Use to read different objects(like list, tuple, dictionary,
class) from file

Syntax- Obj= pickle.load(fileobj)


Read/Write in binary file
Output of read/write
Append
(to enter multiple records)
Output screen
Coding to display records
Output display
Searching record
Output of search
file pointer functions
seek() and tell()
⚫ seek(n,from)- is the file pointer, used to go to
particular position.
n= no of bytes to move.
from= it has 3 values-
0-beginning of file
1- current position
2- end of file
Seek(0) – set file pointer at the beginning
Seek(n)- set file pointer to nth character in the file from
beginning.
Tell()
⚫ tell() - is used to get the byte position from
beginning of the location in the file

⚫ seek() changes position for read and write


pointers and tell() returns an integer value
for the byte offset from the beginning of the
file
Example of seek()
output
Example Tell()
Output screen
Update
Thank
You

You might also like