# How to fix the ValueError: binary mode doesn’t take an encoding argument in Python

The Python errorValueError: binary mode doesn’t take an encoding argument” can be annoying at first, but this article will show you how to fix it. Now let’s begin!

## Why does the “ValueError: binary mode doesn’t take an encoding argument” error occur?

If you get this error, it means you’re attempting to open a text file in binary mode and have passed an encoding argument to the open() function. Because binary mode, by definition, does not require an encoding, adding one will result in an error like the example below.

# Trying to open the txt file in binary mode
with open('data.txt', 'rb', encoding='utf-8') as f:
print(data)

Error:

ValueError: binary mode doesn't take an encoding argument

## How to fix this error?

Let’s quickly look at the open() function before moving on to the debugging section.

The open() function is a Python built-in function that allows you to open a file on your computer. This function normally takes two arguments: the name of the file to be opened and the file’s opening mode. The mode argument is optional, however, it allows you to choose how the file should be interacted with.

Syntax:

open(file, mode)

Parameters:

• file: the file that needs to be opened.
• mode: the mode in which the file should be opened. The mode has the following values: r(read), a(append), w(write), x(create), and t(text), b(binary).

Here are some solutions for the “ValueError: binary mode doesn’t take an encoding argument” error when using the open() function to open a file.

### Do not use the encoding parameter in binary mode

Python reads individual bytes in binary mode. Therefore this mode does not require encoding. If you need to open a file in binary mode, don’t use the encoding parameter for the open() function. See the example below to know how to correct this error.

Suppose we have the data.txt file with the following message: Learn Python at LearnShareIT.

And this is the correct way to read the data.txt file with the open() function:

# Open the txt file in the binary mode without encoding parameter
with open('data.txt', 'rb') as f:
print(data)

Output:

b'Learn Python at LearnShareIT'

### Do not open the file in binary mode if you want to use ‘utf-8’ encoding

By default, text mode requires an encoding parameter, which can be utf-8, Latin-1,… If your file is encoded with a character encoding such as utf-8 and you need to use the encoding parameter in the open() function, open it in text mode.

To specify to open the file in text mode, you need to pass the character ‘r‘ to the mode argument. For example:

# Open the txt file in the binary mode without encoding parameter
with open('data.txt', 'rb') as f:
print(data)

Output:

Learn Python at LearnShareIT

If you only want to read the file, as in the sample code above, you can shorten the code to:

# Open the txt file in text mode
with open('data.txt') as f:
print(data)
Learn Python at LearnShareIT
The open() function uses utf-8 by default, and the mode parameter is ‘r’ by default, so we don’t need to specify those parameters to get the same result as above.