Warning: session_start(): open(/tmp/sess_8f3d0a65c9b1c9ac6b571deaa91bd8d1, 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
How To Solve TypeError: Write() Argument Must Be Str, Not List - LearnShareIT

How To Solve TypeError: Write() Argument Must Be Str, Not List

TypeError: write() argument must be str, not list

TypeError: write() argument must be str, not list” is a common error related to Python files when writing strings. The below explanations can help you know more about this error of causes and solutions.

How does the TypeError: write() argument must be str, not list occur?

Basically, “argument must be str’” means that the parameter or the instance you are passing to the method does not have the type of string type (in Python it is called str). Mostly, the error will happen when you call this method on a file and with the list passed to its parameter in Python. For example:

string = [' LEARNSHAREIT  ']

# Print the list
print(string)

# Write the list to the file
with open('new.txt', 'w') as file:
    file.write(string)

Output in file new.txt

[' LEARNSHAREIT  ']
Traceback (most recent call last):
  File "main.py", line 8, in <module>
    file.write(string)
TypeError: write() argument must be str, not list

The reason behind the lack of the str argument is that you may have mistakes in assuming the arguments in the write method will automatically convert the argument into string type. In fact, the method write() will not convert anything into a string, it just raises the error whenever the parameter is not str. The method print() and write() are most well-known in Python, however, the print() will auto-convert the arguments into strings for you, but the write() does not.

How to solve this error?

Using str()

Fortunately, Python has another method which can help you to convert a list into a string. You can read the syntax and usage of the str() method here. To use this method, just call it on your string variable, which is actually of type list rather than str:

# Create a list of str
string = [' LEARNSHAREIT  ']

# Write the list to the file using str()
with open('new.txt', 'w') as file:
    file.write(str(string))

Output in file new.txt

[' LEARNSHAREIT  ']

The str() method produces a new string representing elements in the list separated by a comma. However, if you may want to replace commas with another character or remove separators, then you can read the next solution.

Using join

Another way to overcome this problem is to convert the list into a string with the commas replaced as another character by calling the method on the separator string you want:

# Declare the seperator as a semicolon
separator = ';'

# Declare our list of str
strings = ['THIS', 'IS', 'LEARN,SHARE,IT']

# Write the list into the file using join()
with open('new.txt', 'w') as file:
    file.write(separator.join(strings))

Output in file new.txt

THIS;IS;LEARN,SHARE,IT

Another example without creating a temporary separator variable:

# Declare the list of str
strings = ['THIS', 'IS', 'LEARN,SHARE,IT']

# Write the list into the file using join()
with open('new.txt', 'w') as file:
    file.write(''.join(strings))

Output in file new.txt

THISISLEARN,SHARE,IT

Summary

We have learned how to solve the TypeError: write() argument must be str, not list. By finding out the reasons and replacing the list with a string rather than a list with the help of third methods, you can easily avoid this error. Good luck to you!

Maybe you are interested:

Leave a Reply

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