TypeError: join() argument must be str or bytes not NoneType

avatar

Borislav Hadzhiev

Last updated: Apr 20, 2022

banner

Photo from Unsplash

TypeError: join() argument must be str or bytes not NoneType #

The Python "TypeError: join() argument must be str, bytes, or os.PathLike object, not 'NoneType'" occurs when we pass a None value to the os.path.join() method. To solve the error, correct the assignment, or provide a fallback value in case the variable stores None.

Here is an example of how the error occurs.

main.py
import os my_path = None # ⛔️ TypeError: join() argument must be str, bytes, or os.PathLike object, not 'NoneType' result = os.path.join('/', my_path)

We passed a None value to the os.path.join() method which caused the error, because the method takes string arguments.

To solve the error, you have to figure out where the variable got assigned a None value 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
import os # 👇️ this function returns None def get_path(): print('home/alice') # ⛔️ TypeError: join() argument must be str, bytes, or os.PathLike object, not 'NoneType' result = os.path.join('/', get_path())

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

main.py
import os def get_path(): return 'home/alice' result = os.path.join('/', get_path()) print(result) # 👉️ '/home/alice'

Use an if statement if you need to check whether a variable doesn't store a None value before using the os.path.join() method.

main.py
import os my_path = None if my_path is not None: result = os.path.join('/', my_path) print(result) else: # 👇️ this runs print('variable stores a None value')

Alternatively, you can provide an empty string as a fallback (if that suits your use case).

main.py
import os my_path = None if my_path is None: my_path = '' result = os.path.join('/', my_path) print(result) # 👉️ '/'
Note that there are many built-in functions (e.g. sort()) that mutate the original object in place and return None.

Make sure you aren't storing the result of calling one in a variable.

The os.path.join method joins one or more paths intelligently.

The method concatenates the provided paths with exactly one directory separator following each non-empty part except the last.

If any of the provided components is an absolute path, all previous components are thrown away and joining continues from the absolute path onwards.

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

main.py
import os def get_path(p): if len(p) > 10: return p my_path = get_path('alice') print(my_path) # ⛔️ TypeError: join() argument must be str, bytes, or os.PathLike object, not 'NoneType' result = os.path.join('/', my_path)

The if statement in the get_path function is only run if the passed in argument has a length greater than 10.

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

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

Conclusion #

The Python "TypeError: join() argument must be str, bytes, or os.PathLike object, not 'NoneType'" occurs when we pass a None value to the os.path.join() method. To solve the error, correct the assignment, or provide a fallback value in case the variable stores None.

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.