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

avatar
Borislav Hadzhiev

Last updated: Apr 8, 2024
3 min

banner

# 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 comes from and correct the assignment.

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

The os.getenv() method returns None if an environment variable with the supplied name doesn't exist.

main.py
import os result = os.getenv('non-existent-variable') print(result) # ๐Ÿ‘‰๏ธ None

os getenv returns none if given name not exists

You can pass a second argument to the method to get a default value when the specified environment variable doesn't exist.

main.py
import os result = os.getenv('non-existent-variable', 'example.txt') print(result) # ๐Ÿ‘‰๏ธ 'example.txt'

supply default value to os getenv

The os.getenv() method returns the value of the specified environment variable or the specified default value if it doesn't exist.

# The most common sources of None in Python

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 return anything return None

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)

function returning nothing returns none

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)

return value from the function

# Checking if the variable doesn't store a None value

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')

check if variable does not store none

Alternatively, you can set the variable to a default value if it is None.

main.py
filename = None if filename is None: filename = 'example.txt' # ๐Ÿ‘ˆ๏ธ set to default value with open(filename, 'r', encoding='utf-8') as f: lines = f.readlines() print(lines)

set variable to default value if none

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.

# Functions that return a value only if a condition is met

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.

main.py
def get_filename(a): if len(a) > 5: return a return 'example.txt' filename = get_filename('b.txt') print(filename) # ๐Ÿ‘‰๏ธ example.txt

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

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.

Copyright ยฉ 2024 Borislav Hadzhiev