You can learn about Python File Processing Programs with Outputs helped you to understand the language better.

Python Programming – Working With Binary Files

You know that text files are composed of octets, or bytes, of binary data whereby each byte represents a character and the end of the file is marked by a special byte pattern, known generically as the end of file, or eof. A binary file contains arbitrary binary data and thus no specific value can be used to identify the end of the file, thus a different mode of operation is required to read these files. Python adds ‘b’ to the mode parameter, like this:
binfile = file(“BinaryFile.bin”,”rb”)

You can use any of the other modes too, simply adding ’b’, i.e.,”wb” to write, “ab” to append. rb+ will open a binary file, for both reading and writing purpose. The file pointer is placed at the beginning of the file when it is opened using rb+ mode, wb+ will open in binary format, for both writing and reading. The file pointer will be placed at the beginning for writing into it, so an existing file will be over-written. A new file can also be created using this model. The ab+ opens a binary file, for both appending and reading. The file pointer is placed at the end of the file, in an already existing file. Using this mode a nonexisting file may be created.

If you want to write structure such as list, dictionary, etc., and also want to read it then you have to use a module in Python known as pickle. Pickling means converting the structure into a byte stream before writing the data into a file. When you want to work on binary files, conversion of data at the time of reading, as well as writing is required. Pickle module can be used to store any kind of object in the file as it allows you to store Python objects with their structure. So for storing data in binary format, you will use the pickle module. First, you need to import the module.

It provides two main methods for the purpose, dump, and load. For the creation of a binary file, you will use a pickle. dump( ) to write the object in the file, which is opened in binary access mode. Once data is stored using dump( ), it can then be used for reading. When you read a file then an opposite operation is to be done known as unpickling. For reading data from a file, you will use pickle.load( ) to read the object from the pickle file.

  • Importing pickle module, required to work on binary file.

Demo of writing binary file.

# Program to write list sequence in binary file
def bin_oper( ) :
import pickle
list = [27,46,72,98,23]
print(“Data added to Binary file successfully.”)
f.close( )
bin_oper( )RUN
Data added to Binary file successfully.

When you try to open listdata.dat in notepad, it will opened as shown in Figure 11.22. To read Binary file use load ( ) function as shown in Figure 11.22.

Python Programming - Working With Binary Files chapter 11 img 1

Demo to read binary file.

# Program to read binary file using load( ) function
def bin_oper( ):
import pickle
f.close( )
bin_oper( )RUN
[ 27 , 46 , 72 , 98 , 23 ]

The pickle module has two methods – dump( ) to write and load( ) to read.

Python comes with several different libraries that allow your Python code to take user input from the command line. The three most common ones are sys. argv, getopt, and argparse.

Leave a Reply

Your email address will not be published. Required fields are marked *