What does the 'b' modifier do when a file is opened using Python?


If we open a file in oython using the b modifier. The file is opened in binary mode using the 'b' modifier. Any file whose format doesn't consist of readable characters is referred to as a "binary" file. Binary files include audio files like MP3s, text formats like Word or PDF, and image files like JPEGs or GIFs. Files are automatically opened in text mode in Python. When choosing a mode, include the letter "b" for binary mode.

By default, the open() function opens a file in text format. As a result, the "wb" mode opens the file in binary format for writing while the "rb" option opens the file in binary format for reading. Binary files are not readable by humans, in contrast to text files. Any text editor can be used to open the data, but it is unusable.

Different modes to open a file in binary format

Following are the different modes to open a file in binary format −

b − A binary mode opening.

ab − It opens the file in binary format, but otherwise is identical to a mode (In a mode, the file is opened for appending. The pointer is at the end of the file if it is present; otherwise, a new file is created for writing).

Example

Following is an example to open a file in ab mode −

# Opening a file file = open("file.txt", "ab") print ("Name of the file: ", file.name) print ("Opening mode : ", file.mode)

Output

Following is an output of the above example

Name of the file:  file.txt
Opening mode :  ab

ab+ − The file opens in binary format, but else is similar to a+ mode (a+ mode enables reading and adding to the file. If the file already exists, the file pointer is at the end of the file; otherwise, a new file is created for reading and writing).

wb − Similar as w mode, but opens the file in binary format (w mode enables writing by opening the file. replaces the existing file and creates a new one if the existing file is absent).

Example

The following code creates a binary file and saves a list of integers in it. Before writing, the list is first transformed into a byte array. bytearray(), an internal function, returns a byte representation of the item.

file=open("files.txt","wb") numbers=[50, 100, 125, 230, 254] array=bytearray(numbers) file.write(array) file.close() print ("File Created")

Output

As an output, we can see a new file created with the name “files.txt”.

File Created

wb+ − The file is opened in binary format, unlike w+ (w+ opens the file in read-only and write-only modes; rest is similar to w mode).

rb − It opens the file in binary mode, but else is identical to r mode (The r mode i.e. read-only mode is used to open a file. The file's pointer is located at the start of the file. Additionally, this is the default mode).

Example

file = open('file.txt', 'rb') file_info = file.read() file.close() print ("File Created")

Output

As an output, we can see a new file created with the name “files.txt”.

File Created

rb+ − Similar to r+ mode, only opens the file in binary mode instead (r+ mode opens the file so that it can be read and written to. The file's starting point is where the pointer is).

Note − The example code for all the modes will be same, just replace the modes i.e rb, rb+, wb, wb+ e.tc. as needed.

Updated on: 14-Nov-2022

10K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements