File Handling Binary File
File Handling 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.
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)
⚫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
Input
2. load()
Use to read different objects(like list, tuple, dictionary,
class) from file