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 -
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. Next TopicWhat is a negative indexing in python |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India