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

avatar

Borislav Hadzhiev

Last updated: Apr 20, 2022

banner

Photo from Unsplash

TypeError: write() argument must be str, not None (Python) #

The Python "TypeError: write() argument must be str, not None" occurs when we pass a None value to the write() method. To solve the error, correct the assignment and pass a string to the write() method.

typeerror write argument must be str not none

Here is an example of how the error occurs.

main.py
with open('example.txt', 'w', encoding='utf-8') as my_file: my_str = None my_file.write(my_str)

We passed a None value to the write method which caused the error.

The method takes a string and writes it to the file.

To solve the error, you have to figure out where the None value comes from and correct the assignment.

The most common sources of None values are:

  1. Having a function that doesn't return anything (returns None implicitly).
  2. Explicitly setting a variable to None.
  3. Assigning a variable to the result of calling a built-in function that doesn't return anything.
  4. Having a function that only returns a value if a certain condition is met.

Functions that don't explicitly return a value return None.

main.py
# 👇️ this function returns None def get_str(): print('hello world') with open('example.txt', 'w', encoding='utf-8') as my_file: my_str = get_str() print(my_str) # 👉️ None # ⛔️ TypeError: write() argument must be str, not None my_file.write(my_str)

You can use a return statement to return a value from a function.

main.py
def get_str(): return 'hello world' with open('example.txt', 'w', encoding='utf-8') as my_file: my_str = get_str() print(my_str) # 👉️ "hello world" my_file.write(my_str)

Use an if statement if you need to check whether a variable doesn't store a None value before writing it to the file.

main.py
with open('example.txt', 'w', encoding='utf-8') as my_file: my_str = None if my_str is not None: my_file.write(my_str)

Alternatively, you can provide an empty string as a fallback.

main.py
with open('example.txt', 'w', encoding='utf-8') as my_file: my_str = None if my_str is None: my_str = '' my_file.write(my_str)

Another common cause of the error is having a function that returns a value only if a condition is met.

main.py
def get_string(a): if len(a) > 3: return a my_string = get_string('hi') print(my_string) # 👉️ None

The if statement in the get_string function is only run if the passed in string has a length greater than 3.

In all other cases, the function doesn't return anything and ends up implicitly returning None.

To solve the error in this scenario, you either have to check if the function didn't return None or return a default value if the condition is not met.

main.py
def get_string(a): if len(a) > 3: return a return '' my_string = get_string('hi') print(my_string) # 👉️ ""

Now the function is guaranteed to return a string regardless if the condition is met.

Conclusion #

The Python "TypeError: write() argument must be str, not None" occurs when we pass a None value to the write() method. To solve the error, correct the assignment and pass a string to the write() method.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.