Python Data Persistence – Write/Read Binary File
Read binary file in python: When the mode parameter to open ( ) function is set to ‘w’ (or ‘a’) value, the file is prepared for writing data in text format. Hence write ( ) and writelines () methods send string data to the output file stream. The file is easily readable using any text editor. However, other computer files that represent pictures (.jpg), multimedia (.mp3, .mp4), executables (.exe, .com), databases (.db, .sqlite) will not be readable if opened using notepad like text editor because they contain data in binary format.
Python’s open( ) function lets you write binary data to a file. You need to add ‘b’ to the mode parameter. Set mode to ‘wb’ to open a file for writing binary data. Naturally, such a file needs to be read by specifying ‘rb’ as file opening mode.
In the case of ‘wb’ mode parameter, the write () method doesn’t accept a string as an argument. Instead, it requires a bytes object. So, even if you are writing a string to a binary file, it needs to be converted to bytes first. The encode ( ) method of string class converts a string to bytes using one of the defined encoding schemes, such as ‘utf-8’ or ‘utf-16’.
- Python open() Function with Examples
- Python Program to Remove Duplicate Lines from Text File
- Python Program to Read Char-by-Char from File
>>> f=open('binary.dat' , 'wb') >>> data='Hello Python' >>> encoded=data.encode(encoding='utf-16') >>> encoded b'\xff\xfeH\x00e\x001\x001\x00o\x00 \x00P\x00y\x00t\ x00h\x00o\x00n\x00' >>> f.write(encoded) 26 >>> f.close( )
To read back data from the binary files, the encoded strings will have to be decoded for them to be used normally, like printing it.
> > > f=open('binary.dat', 'rb') >>> encoded=f.read( ) >>> data=encoded.decode(encoding='utf-16') >>> print (data) Hello Python >>> f.close( )
If you have to write numeric data to a binary file, number object is first converted to bytes and then provided as argument to write() method
>>> f=open('binary.dat','wb') >>> num=1234 >>> binl=num.to_bytes(16, 'big') >>> f.write(bin1) >>> f.close( )
To read integer data from binary file, it has to be extracted from bytes in the file.
>>> f=open( ' binary.dat' , 'rb') >>> data=f.read( ) >>> num=int.from_bytes(data,'big') > > > num 1234 >>> f.close( )
In the to_bytes () and from_bytes () function, ‘big’ is the value of the byte order parameter. Writing float data to a binary file is a little complicated. You’ll need to use the built-in struct module and pack( ) function from it to convert float to binary. Explanation of struct module is beyond the scope of this book.
>>> import struct > > > numl=1234.56 >>> f=open('binary.dat','wb') >>> binfloat = struct .pack ('f ' num1) > > > f . write (binfloat) 4 > > > f.close ( )
To read back binary float data from file, use unpack () function.
>>> f=open('binary.dat','rb') >>> data=f.read( ) >>> numl=struct.unpack('f', data) >>> print (num1) (1234.56005859375,) >>> f.close( )