InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

Menu
  • Home
  • Sitemap

Python Programming Language Best Tutorials and Code Examples

Learn Python Right Now!
Home
Python
Reading and Writing Binary Data in Python
Python

Reading and Writing Binary Data in Python

InfinityAdministrator November 20, 2016

You need to read or write binary data in Python, such as that found in images, sound files, and so on. Use the open() function with mode rb or wb to read or write binary data.

For example:

1
2
3
4
5
6
7
# Read the entire file as a single byte string
with open('filebinary.bin', 'rb') as fl:
    allBinData = fl.read()
 
# Write binary data to a file
with open('filebinary.bin', 'wb') as fl:
    fl.write(b'Hello World')

When reading binary, it is important to stress that all data returned will be in the form of byte strings, not text strings. Similarly, when writing, you must supply data in the form of objects that expose data as bytes (e.g., byte strings, bytearray objects, etc.).

When reading binary data, the subtle semantic differences between byte strings and text strings pose a potential gotcha. In particular, be aware that indexing and iteration return integer byte values instead of byte strings.

For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
>>> # Text string
>>> text = "Hello"
>>> text[0]
'H'
>>> for c in text:
...     print(c)
...
H
e
l
l
o
...
>>> # Byte string
>>> byteStr = b"Hello"
>>> byteStr[0]
72
>>> for c in byteStr:
...     print(c)
...
72
101
108
108
111
...
>>>

If you ever need to read or write text from a binary-mode file, make sure you remember to decode or encode it.

For example:

1
2
3
4
5
6
7
with open('mybinfile.bin', 'rb') as fl:
    data = fl.read(128)
    text = data.decode('utf-8') # from binary to text
 
with open('mybinfile.bin', 'wb') as fl:
    text = 'Hello InfinityQuest!'
    fl.write(text.encode('utf-8')) #from text to binary

A lesser-known aspect of binary I/O is that objects such as arrays and C structures can be used for writing without any kind of intermediate conversion to a bytes object.

For example:

1
2
3
4
import array
numbs = array.array('i', [1, 2, 3, 4, 5, 6, 9]) # must be same type
with open('myData.bin','wb') as fl:
    fl.write(numbs)

This applies to any object that implements the so-called “buffer interface” which directly exposes an underlying memory buffer to operations that can work with it. Writing binary data is one such operation.

Many objects also allow binary data to be directly read into their underlying memory using the readinto() method of files.

For example:

1
2
3
4
5
6
7
8
9
>>> import array
>>> abc = array.array('i', [0, 0, 0, 0, 0, 0, 0, 0, 0])
>>> with open('dataBinary.bin', 'rb') as fll:
...     fll.readinto(abc)
...
18
>>> abc
array('i', [5, 6, 7, 2, 9, 9, 0, 0, 0])
>>>

However, great care should be taken when using this technique in Python, as it is often platform specific and may depend on such things as the word size and byte ordering (i.e., big endian versus little endian).

We recommend next video tutorial:

 

Share
Tweet
Email
Prev Article
Next Article
Tags:binary data python how to write binary data in Python read binary data python tutorial

Related Articles

Generating a Range of IP Addresses from a CIDR Address in Python
Suppose you have a CIDR network address such as “123.45.67.89/27,” …

Generating a Range of IP Addresses from a CIDR Address in Python

Turning a Dictionary into XML in Python
You want to take the data in a Python dictionary …

Turning a Dictionary into XML in Python

About The Author

InfinityAdministrator
InfinityAdministrator

Leave a Reply

Cancel reply

Recent Tutorials InfinityQuest

  • Adding New Features to bash Using Loadable Built-ins in bash
    Adding New Features to bash Using Loadable …
    June 27, 2017 0
  • Getting to the Bottom of Things in bash
    Getting to the Bottom of Things in …
    June 27, 2017 0

Recent Comments

  • fer on Turning a Dictionary into XML in Python
  • mahesh on Turning a Dictionary into XML in Python

Categories

  • Bash
  • PHP
  • Python
  • Uncategorized

InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

About Us

Start learning your desired programming language with InfinityQuest.com.

On our website you can access any tutorial that you want with video and code examples.

We are very happy and honored that InfinityQuest.com has been listed as a recommended learning website for students.

Popular Tags

binary data python CIDR convert string into datetime python create xml from dict python dictionary into xml python how to create xml with dict in Python how to write binary data in Python IP Address read binary data python tutorial string as date object python string to datetime python

Archives

  • June 2017
  • April 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
Copyright © 2021 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
Programming Tutorials | Sitemap