Warning: session_start(): open(/tmp/sess_de80546c040d484ae3a2de0b7657cf58, O_RDWR) failed: Disk quota exceeded (122) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: session_start(): Failed to read session data: files (path: /tmp) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_mkdir() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 562

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230
Solutions for TypeError: write() argument must be str, not bytes in Python - LearnShareIT

Solutions for TypeError: write() argument must be str, not bytes in Python

TypeError: write() argument must be str, not bytes in Python

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:

Leave a Reply

Your email address will not be published. Required fields are marked *