What does the 'b' Modifier do When a File is Opened Using Python?

Introduction:

In this tutorial, we are learning the 'b' modifier when a file is opened using Python. If we use the b modifier to open the file in Python. Using the "b" modifier to open the file in binary format. Any file in a format that does not contain human-readable characters is called a "binary" file. In Python, files are opened in text format. When choosing a format, add the letter "b" for binary format. By default, the open() function is used to opens the file as a text. So, the "wb" option is used to opens files in binary format for writing. While the "rb" option is used to opens a file in binary format for reading purpose. Compared to a text, the binary files cannot read humans. Text can open the file, but it is not usable.

Different modes for opening a file in the binary format using Python:

There are two different modes for opening a file in the binary format using Python, which are given below -

  1. b: It opens the file in binary mode.
  2. ab: It opens the file in binary mode. Otherwise, it is identical to the mode (in the mode, the file is open for appending. If valid, the pointer is at the end of the file. otherwise, it creates a new file for writing).
  3. ab+: It opens the file in binary mode. But it is not the same as a+ format (a + format can be read and appended to the file. If the file already exists, then the file pointer is the end of files; otherwise, new files are created for reading and writing).
  4. wb: It opens the file in binary mode. It is similar to the w mode. w mode opens the file and allows writing. If the existing file does not exist, then it is replaced by a new created file.
  5. wb+: It opens the file in binary mode. It is similar to the w+ mode. w+ open files in read-only modes and write-only modes, but the rest is similar to w mode.
  6. rb: It opens the file in binary mode but is otherwise the same as r mode. The r mode is the read-only mode used to open the file. The file pointer is at the beginning of the file. This is also the default mode.
  7. rb+: It opens the file in binary mode. It is similar to the r+ mode. The r+ mode opens the file so it can be read and written. The starting point of the file is where the pointer is located.

Program Code 1:

Here, we give a program code for opening a file in the binary format using ab mode in Python. The code is given below -

Output:

Now, we run the above code and find the opening of the given file. The output is given below -

 
The name of the given file is: untitle.txt
The opening mode of the given file is: ab   

Program Code 2:

Here, we give a program code for opening a file in the binary format using wb mode in Python. The code below creates a binary file and stores a list of numbers. Before writing, the list is first converted into a byte array. The bytearray() function is returns a byte representation of an object. The code is given below -

Output:

Now we run the above code and find the result from it. The output is given below -

 
The file is created   

Program Code 3:

Here, we give a program code for opening a file in the binary format using rb mode in Python. The code is given below -

Output:

Now we run the above code and find the result from it. The output is given below -

 
The file is created   

Conclusion:

In this tutorial, we learn the 'b' modifier when a file is opened using Python. We also learn various file opening modes, such as n, ab, ab+, wb, wb+, rb, and rb+ mode, and some program code for this.