pathlib is a module available in Python. This module provides classes representing file system paths on different operating systems. This article will show the cause of the error Unsupported operand type(s) for +: ‘PosixPath’ and ‘str’ and how to fix it. For example, use the “
type()” function to check the data and use the “
str()” to convert the data. Let’s solve it together.
What causes the error Unsupported operand type(s) for +: ‘PosixPath’ and ‘str’ in Python
Unsupported operand type(s) for +: ‘PosixPath’ and ‘str’ occurs when you use the “+” sign to concatenate a PosixPath object to a string, which is not possible because you are concatenating a string to an object. The example below illustrates how the Error happened.
from pathlib import Path path = Path('/images') # Error occurred because we concatenated PosixPath object with the string imagePath = path + '/home/banner.jpg'
TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
In the above example, the variable path receives an object returned from the “
Path()” function. Next, we misunderstand that this variable is a string, we concatenate this string with a string to build the
imagePath, and so the Error occurs.
Solutions for Unsupported operand type(s) for +: ‘PosixPath’ and ‘str’ error
To solve this Error, first, we need to check the data type of the variable to be appended to the string. If the data type is not a string, then we need to convert that object to a string type.
Check the type of data.
Use the “
type()” function to check the variable’s data type before using it to append to a string.
type()" takes 1 parameter, that is the object that we need to check the type.
This function will return the data type of the object we passed in, helping us determine whether to append to the string or not.
from pathlib import Path path = Path('/images') # Use "type()" to determine the data type print(type(path))
Use “str()” function
After using the “
type()” function to check, we know that the object is not of type string. We use the “
str()” function to convert the object to a string and then append it to the string we want to construct the path.
The passed parameter is whatever object we want to convert to a string.
The return value will be a string converted from the object we passed in.
from pathlib import Path path = Path('/images') # Use "str()" to convert object to string pathAfterConverted = str(path) print(pathAfterConverted)
After converting the object to a string, we may append the transformed string to the one we want to build to generate the desired path
from pathlib import Path path = Path('/images') pathAfterConverted = str(path) # Concatenate the converted string to the string to be built imagePath = pathAfterConverted + '/home/banner.jpg' print(imagePath)
You see, with this simple trick, we solved the problem. Pre-check the data if we need to know the data type before we use it. We hope you like it. Good luck to you!
Maybe you are interested:
- TypeError: unsupported operand type(s) for /: list and int
- Unsupported operand type(s) for *: float and decimal.Decimal
- TypeError: unsupported operand type(s) for -: str and int
Carolyn Hise has three years of software development expertise. Strong familiarity with the following languages is required: Python, Typescript/Nodejs, .Net, Java, C++, and a strong foundation in Object-oriented programming (OOP).