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

PythonServer Side ProgrammingProgramming

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 filefile = 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.txtOpening 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.

Sarika Singh

Updated on: 14-Nov-2022

12K+ Views

  • Related Articles
  • What does the \'U\' modifier do when a file is opened using Python?
  • What are the modes a file can be opened using Python?
  • What does the modifier transient in Java do?
  • What does the modifier volatile in Java do?
  • What does synchronized modifier do in Java?
  • What does abstract modifier in Java do?
  • How to close an opened file in Python?
  • What does opening a file actually do on Linux?
  • How to prevent Body from scrolling when a modal is opened using jQuery?
  • What does \'is\' operator do in Python?
  • Check if the camera is opened or not using OpenCV Python
  • How do we specify the buffer size when opening a file in Python?
  • How do we do a file upload using Python CGI Programming?
  • What does the 'b' character do in front of a string literal in Python?
  • What should you absolutely never do when using Python?
Kickstart Your Career

Get certified by completing the course

Get Started

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

Advertisem*nts

What does the 'b' modifier do when a file is opened using Python? (2024)
Top Articles
Best Apex Legends VPN for Lower Ping and Zero Lag in 2024
Time-to-kill
Po Box 7250 Sioux Falls Sd
Kansas City Kansas Public Schools Educational Audiology Externship in Kansas City, KS for KCK public Schools
Citibank Branch Locations In Orlando Florida
The Ivy Los Angeles Dress Code
THE 10 BEST River Retreats for 2024/2025
Nyuonsite
Red Heeler Dog Breed Info, Pictures, Facts, Puppy Price & FAQs
Richmond Va Craigslist Com
Wordle auf Deutsch - Wordle mit Deutschen Wörtern Spielen
The Murdoch succession drama kicks off this week. Here's everything you need to know
Leader Times Obituaries Liberal Ks
Lehmann's Power Equipment
Td Small Business Banking Login
Busted Campbell County
Poe Str Stacking
Melendez Imports Menu
Little Rock Skipthegames
Best Boston Pizza Places
Marokko houdt honderden mensen tegen die illegaal grens met Spaanse stad Ceuta wilden oversteken
Pacman Video Guatemala
Jamielizzz Leaked
CohhCarnage - Twitch Streamer Profile & Bio - TopTwitchStreamers
Eegees Gift Card Balance
Fairwinds Shred Fest 2023
Melissa N. Comics
How to Use Craigslist (with Pictures) - wikiHow
Unm Hsc Zoom
Pnc Bank Routing Number Cincinnati
The Ride | Rotten Tomatoes
Tgh Imaging Powered By Tower Wesley Chapel Photos
Car Crash On 5 Freeway Today
John F Slater Funeral Home Brentwood
Chatropolis Call Me
Craigslist Ludington Michigan
Skip The Games Grand Rapids Mi
Tyler Perry Marriage Counselor Play 123Movies
Clausen's Car Wash
Linkbuilding uitbesteden
Powerspec G512
Catchvideo Chrome Extension
Craigslist Mendocino
25 Hotels TRULY CLOSEST to Woollett Aquatics Center, Irvine, CA
Terrell Buckley Net Worth
Verizon Forum Gac Family
Minecraft Enchantment Calculator - calculattor.com
Basic requirements | UC Admissions
Obituary Roger Schaefer Update 2020
Ihop Deliver
Latest Posts
Article information

Author: Catherine Tremblay

Last Updated:

Views: 5601

Rating: 4.7 / 5 (47 voted)

Reviews: 94% of readers found this page helpful

Author information

Name: Catherine Tremblay

Birthday: 1999-09-23

Address: Suite 461 73643 Sherril Loaf, Dickinsonland, AZ 47941-2379

Phone: +2678139151039

Job: International Administration Supervisor

Hobby: Dowsing, Snowboarding, Rowing, Beekeeping, Calligraphy, Shooting, Air sports

Introduction: My name is Catherine Tremblay, I am a precious, perfect, tasty, enthusiastic, inexpensive, vast, kind person who loves writing and wants to share my knowledge and understanding with you.