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
Improve Your Relationship With Money | Money Health
Risk/Reward in Trading - The Complete Guide for Traders
Craigslist Livingston Montana
Hotels Near 6491 Peachtree Industrial Blvd
417-990-0201
Craigslist Monterrey Ca
Craftsman M230 Lawn Mower Oil Change
Immobiliare di Felice| Appartamento | Appartamento in vendita Porto San
Recent Obituaries Patriot Ledger
Www Movieswood Com
Hssn Broadcasts
D10 Wrestling Facebook
979-200-6466
Canvas Nthurston
Imagetrend Inc, 20855 Kensington Blvd, Lakeville, MN 55044, US - MapQuest
Roster Resource Orioles
Spoilers: Impact 1000 Taping Results For 9/14/2023 - PWMania - Wrestling News
Kp Nurse Scholars
Whitefish Bay Calendar
Understanding Genetics
Adt Residential Sales Representative Salary
Dallas Mavericks 110-120 Golden State Warriors: Thompson leads Warriors to Finals, summary score, stats, highlights | Game 5 Western Conference Finals
Titanic Soap2Day
Craigslist Northfield Vt
Jeffers Funeral Home Obituaries Greeneville Tennessee
Workshops - Canadian Dam Association (CDA-ACB)
Feathers
Safeway Aciu
Jamielizzz Leaked
Past Weather by Zip Code - Data Table
Sony Wf-1000Xm4 Controls
Greater Orangeburg
Emiri's Adventures
About | Swan Medical Group
Royal Caribbean Luggage Tags Pending
Blackstone Launchpad Ucf
My.lifeway.come/Redeem
Td Ameritrade Learning Center
Craigslist Ludington Michigan
Tyler Perry Marriage Counselor Play 123Movies
Craigslist Freeport Illinois
2007 Jaguar XK Low Miles for sale - Palm Desert, CA - craigslist
Kenner And Stevens Funeral Home
Promo Code Blackout Bingo 2023
Searsport Maine Tide Chart
Craigslist Mendocino
2294141287
Powah: Automating the Energizing Orb - EnigmaticaModpacks/Enigmatica6 GitHub Wiki
Nfhs Network On Direct Tv
Southern Blotting: Principle, Steps, Applications | Microbe Online
Haunted Mansion Showtimes Near The Grand 14 - Ambassador
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.