Expected str, bytes or os.PathLike object, not NoneType

avatar

Borislav Hadzhiev

Last updated: Apr 20, 2022

banner

Photo from Unsplash

Expected str, bytes or os.PathLike object, not NoneType #

The Python "TypeError: expected str, bytes or os.PathLike object, not NoneType" occurs when we try to open a file but provide a None value for the filename. To solve the error, figure out where the None value comes from and correct the assignment.

typeerror expected str bytes or os pathlike object not nonetype

Here is an example of how the error occurs.

main.py
filename = None # ⛔️ TypeError: expected str, bytes or os.PathLike object, not NoneType with open(filename, 'r', encoding='utf-8') as f: lines = f.readlines() print(lines)

We passed a None value for the filename to the open() function which caused the error.

The open() function expects a string for the first argument, e.g. example.txt.

To solve the error, we have to figure out where the None value is coming from and correct the assignment.

Make sure you are setting your environment variables correctly as that often causes the error.

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_filename(): print('example.txt') # ⛔️ TypeError: expected str, bytes or os.PathLike object, not NoneType with open(get_filename(), 'r', encoding='utf-8') as f: lines = f.readlines() print(lines)

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

main.py
def get_filename(): return 'example.txt' with open(get_filename(), 'r', encoding='utf-8') as f: lines = f.readlines() print(lines)

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

main.py
filename = None if filename is not None: with open(filename, 'r', encoding='utf-8') as f: lines = f.readlines() print(lines) else: print('filename stores a None value')
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.

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

main.py
def get_filename(a): if len(a) > 5: return a filename = get_filename('b.txt') print(filename) # 👉️ None

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

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.

Conclusion #

The Python "TypeError: expected str, bytes or os.PathLike object, not NoneType" occurs when we try to open a file but provide a None value for the filename. To solve the error, figure out where the None value comes from and correct the assignment.

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.