Select Page

How to Solve Python ValueError: binary mode doesn’t take an encoding argument

by | Programming, Python, Tips

This error occurs when you pass the encoding keyword argument to an open() function call while reading or writing to a file in binary mode. The encoding keyword argument is only suitable for reading or writing in text mode.

If you are accessing a file in binary mode you can solve this error by removing the encoding argument. For example,

with open('array.bin', 'rb') as f:

    arr = f.read()

    num = list(arr)

    print(num)

If you want to access a file in text mode, you can remove the ‘b’ from the mode argument. For example,

with open('file.txt', 'r', encoding='utf-8') as f:

    content = f.readlines()

    print(content)

This tutorial will go through the error in detail and how to solve it with code examples.


ValueError: binary mode doesn’t take an encoding argument

In Python, a value is the information stored within a particular object. We will encounter a ValueError in Python when we use an operation or function that receives an argument with the right type but an inappropriate value.

The open function can have two modes to access files: binary and text mode. If we open a file in binary mode, we do not need an encoding argument. Encoding arguments are only appropriate when reading or writing to text files. If encoding is not specified, then Python uses the default encoding, which is platform-depenedent.

Example

Let’s look at an example of writing to a binary file. The open() function opens a file in text former by default. We need to add a ‘b‘ to the mode parameter when calling the open function. ‘rb‘ mode opens the file in binary format for reading and ‘wb‘ mode opens the file in binary format for writing.

with open('array.bin', 'wb', encoding='utf-8') as f:

    num=[2,4,6,8,10]

    arr=bytearray(num)

    f.write(arr)

In the above code, we open a binary file using a context manager, and then write a byte array to the file.

Let’s run the code to see what happens:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Input In [6], in <cell line: 1>()
----> 1 with open('array.bin', 'wb', encoding='utf-8') as f:
      2     num=[2,4,6,8,10]
      3     arr=bytearray(num)

ValueError: binary mode doesn't take an encoding argument

The error occurred because we specified the encoding keyword argument whilst in binary mode. The encoding setting is only needed for text mode.

Solution

We can solve this error by removing the encoding argument. Let’s look at the revised code:

with open('array.bin', 'wb') as f:

    num=[2,4,6,8,10]

    arr=bytearray(num)

    f.write(arr)

When we run the code we will create a file called array.bin in our working directory containing the byte array. We can load the data into our program using open for reading and binary mode ‘rb‘.

We can convert the byte array back to a list of numbers using the list() function.

with open('array.bin', 'rb') as f:

    arr = f.read()

    num = list(arr)

    print(num)

Let’s run the code to get the original list of numbers.

[2, 4, 6, 8, 10]

Summary

Congratulations on reading to the end of this tutorial!

For further reading on Python ValueErrors, go to the article:

To learn more about Python for data science and machine learning, go to the online courses page on Python for the most comprehensive courses available.

Have fun and happy researching!