You try writing data in bytes to a file. However, you receive a message, which is TypeError: write() argument must be str, not bytes in Python. Don’t worry because this is a common error that most new Python developers get. This article will explain the cause and solutions to fix it!
The cause of the TypeError: write() argument must be str, not bytes in Python
You get the message error: “TypeError: write() argument must be str, not bytes” in Python when trying to write data in bytes to your text file.
In the sample below, you want to write the bytes: b'learnshareit.com'
to the file test.txt
.
with open('test.txt', 'w') as testFile: testFile.write(b'learnshareit.com')
The message will show:
testFile.write(b'learnshareit.com')
TypeError: write() argument must be str, not bytes
It is easy to understand. Python requires you to use the Binary Mode before you write bytes.
The 2nd parameter in the open()
function is 'w'
. This allows you to write strings to your file only.
Solution for this error
The solution for the error is very easy. As mentioned, you must use the Binary Mode to write a byte to the file.
All you need to do is changing the 2nd parameter in the open()
function from 'w'
to 'wb'.
'wb'
stands for “write byte”. It means that you are “writing in the Binary Mode”. After making the change, you will see no error from your Python program anymore.
In the sample below, Python starts writing the bytes: b'learnshareit.com'
to the file test.txt
:
# Open the test.txt in the Binary Mode with open('test.txt', 'wb') as testFile: testFile.write(b'learnshareit.com')
Open the test.txt
file, and you will see the work done:
learnshareit.com
Please remember that we read and write data as ‘bytes’
in the Binary Mode. Python does not make any changes to the data as it is written in the file. Therefore, the Binary Mode should only be used for any file that does not contain text.
If you want to open a binary file, use 'rb'
instead of 'r'
in the 2nd parameter of the open()
function.
You can see the sample code below:
# Open the test.txt in the Binary Mode with open('test.txt', 'wb') as testFile: testFile.write(b'learnshareit.com') # Read the test.txt in the Binary Mode with open('test.txt', 'rb') as testFile: print(testFile.read())
Summary
The TypeError: write() argument must be str, not bytes in Python occurs if you write bytes to a text file. The cause of this error is that you put the ‘w’ in the 2nd parameter of the open()
function. Just replace the 'w'
with 'wb'
, and your Python program will work properly!
Maybe you are interested:
- TypeError: unsupported operand type(s) for /: list and int
- TypeError: list indices must be integers or slices not float
- Unsupported operand type(s) for +: ‘PosixPath’ and ‘str’
I am William Nguyen and currently work as a software developer. I am highly interested in programming, especially in Python, C++, Html, Css, and Javascript. I’ve worked on numerous software development projects throughout the years. I am eager to share my knowledge with others that enjoy programming!