Last updated: Apr 8, 2024
Reading timeยท3 min
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.
Here is an example of how the error occurs.
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
.
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.
import os result = os.getenv('non-existent-variable') print(result) # ๐๏ธ None
You can pass a second argument to the method to get a default value when the specified environment variable doesn't exist.
import os result = os.getenv('non-existent-variable', 'example.txt') print(result) # ๐๏ธ 'example.txt'
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
values are:
None
implicitly).None
.Functions that don't explicitly return a value return None
.
# ๐๏ธ 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.
def get_filename(): return 'example.txt' with open(get_filename(), 'r', encoding='utf-8') as f: lines = f.readlines() print(lines)
None
valueUse an if
statement if you need to
check whether a variable doesn't store a None value
before opening the file.
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')
Alternatively, you can set the variable to a default value if it is None
.
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)
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.
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
.
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.
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.