Important Notice:

Binary Mode — b

Binary Mode — b

4 views 3 min read

Binary Mode — b   :-

 Binary Mode का उपयोग किसी File को Binary Data के रूप में Read या Write करने के लिए किया जाता है। इसमें File का Data bytes के रूप में handle होता है, न कि सामान्य Text (str) के रूप में।

इसका उपयोग images, audio, video और अन्य binary files के साथ काम करने में किया जाता है।

b सामान्यतः किसी अन्य mode के साथ जोड़ा जाता है, जैसे rb, wb, ab।

English: Binary Mode is used to read or write files in the form of binary data. In this mode, data is handled as bytes rather than normal text (str).

It is commonly used for images, audio, video, and other binary files.

The b character is generally combined with another file mode, such as rb, wb, or ab.

Syntax -

file = open("filename", "modeb")

या

file = open("filename", "binary_mode")
 

Examples:

  • rb – Read Binary

  • wb – Write Binary

  • ab – Append Binary

  • rb+ – Read and Write Binary

  • wb+ – Write and Read Binary

  • ab+ – Append and Read Binary

Important: b अकेले सामान्यतः File Mode के रूप में उपयोग नहीं किया जाता। इसे r, w, a जैसे modes के साथ जोड़ा जाता है।

Read Binary Mode — rb

हिंदी में: rb Mode का उपयोग किसी Binary File को Read करने के लिए किया जाता है। इस Mode में File पहले से मौजूद होनी चाहिए।

English: The rb mode is used to read the contents of an existing binary file.

Syntax:

file = open("image.jpg", "rb")

Example:

file = open("photo.jpg", "rb")

data = file.read()

print(type(data))
print(data[:10])

file.close()

Possible Output:

<class 'bytes'>
b'\xff\xd8\xff\xe0\x00\x10JFIF'

Explanation:

  • open("photo.jpg", "rb") – Binary File को Read Mode में Open करता है।

  • file.read() – पूरी File का Data Read करता है।

  • type(data) – Data का Type बताता है।

  • data[:10] – Data के शुरुआती 10 Bytes दिखाता है।

  • file.close() – File को Close करता है।

Note: Output File के वास्तविक Content पर निर्भर करेगा। ऊपर दिया गया Output केवल JPEG File का उदाहरण है।

 Write Binary Mode — wb :-

 wb Mode का उपयोग Binary File में Data Write करने के लिए किया जाता है। यदि File मौजूद नहीं है तो नई File बन जाती है। यदि File पहले से मौजूद है तो उसका पुराना Content हट जाता है।

English: The wb mode is used to write bytes into a binary file. It creates a new file if it does not exist and truncates an existing file.

Syntax:

file = open("data.bin", "wb")

Example:

file = open("data.bin", "wb")

data = b"Hello Python"

file.write(data)

file.close()

Explanation:

  • open("data.bin", "wb") – Binary File को Write Mode में Open करता है।

  • b"Hello Python" – यह Bytes Literal है।

  • file.write(data) – Bytes को File में Write करता है।

  • file.close() – File को Close करता है।

Important: Binary Mode में write() के लिए bytes या compatible bytes-like object देना आवश्यक है।

 Append Binary Mode — ab :-

 ab Mode का उपयोग Binary File के अंत में नया Binary Data जोड़ने के लिए किया जाता है। इसमें पहले से मौजूद Data Delete नहीं होता।

English: The ab mode is used to append binary data at the end of an existing file. It creates the file if it does not exist.

Example:

file = open("data.bin", "ab")

file.write(b" Welcome")

file.close()

Explanation:

  • ab – File को Append Binary Mode में Open करता है।

  • write() – नया Bytes Data File के अंत में जोड़ता है।

  • पुराना Data सुरक्षित रहता है।

 

Related Notes