# TypeError: expected str, bytes or os.PathLike object, not NoneType in Python

This article will help you solve the error “TypeError: expected str, bytes or os.PathLike object, not NoneType” in Python. If you are getting in trouble with this problem, read this article carefully. Let’s start by first discovering the error “TypeError: expected str, bytes or os.PathLike object, not NoneType”.

## Reason for error “TypeError: expected str, bytes or os.PathLike object, not NoneType” in Python

You receive the message error “TypeError: expected str, bytes or os.PathLike object, not NoneType” when reading a file whose path is a NoneType object. It means that the path to the file is somehow wrong and returns the none value. For example, the following code will raise the error.

Code:

import os

# Create a path to the target file
filePath = os.path.join(os.getenv("HOME"), "Documents\T5\example.txt")

# Open the file in read mode
with open(filePath, 'r') as file:
print(file.read())

Result:

Traceback (most recent call last):
File "c:\Users\Ha May\Documents\T5\at.py", line 4, in <module>
filePath = os.path.join(os.getenv("HOME"), "Documents\T5\example.txt")
TypeError: expected str, bytes or os.PathLike object, not NoneType

In this example, users want to access the environment variable named “HOME” and then merge it with the relative path to create an absolute path to read the file. Unfortunately, there is no environment variable named “HOME” in the Windows Operating System. So, the error occurs.

Code:

import os

path = os.getenv("HOME") # <-- None
print(path)

Result:

None

## Solution to the error

Remember the key that the replace() method is only available to string objects. Below are a few solutions to handle the problem.

### Use the correct environment variables

The environment variables in the Windows Operating System are “HOMEDRIVE” and “HOMEPATH”. The “HOMEDRIVE” environment variable is the C drive, and the rest is the path to \Users\User_name. To fix the error, you just need to change the environment variable from “HOME” to “HOMEDRIVE” or “HOMEPATH”. For example:

Code:

import os

# Create a path to the target file
filePath = os.path.join(os.getenv("HOMEPATH"), "Documents\T5\example.txt")

# Open the file in read mode
with open(filePath, 'r') as file:
print(file.read())

Result:

Merry Christmas!
Learn Share IT

### Use the absolute path

Alternatively, you can use the absolute path of the file to point to the file. The code below is an example of using the absolute path.

Code:

# Create an absolute path to the target file
filePath = r"C:\Users\Ha May\Documents\T5\example.txt"

# Open the file in read mode
with open(filePath, 'r') as file:
print(file.read())

Result:

Merry Christmas!
Learn Share IT

## Summary

In summary, the error “TypeError: expected str, bytes or os.PathLike object, not NoneType” occurs because you make a mistake when creating the path to the target file. To eliminate the error, we strongly recommend using the absolute path. In case you want to use the environment variable, remember that “HOMEDRIVE” and “HOMEPATH” are the correct environment variables in the Windows Operating System.