TypeError: expected str, bytes or os.PathLike object, not tuple

avatar

Borislav Hadzhiev

Wed Apr 20 20222 min read

banner

Photo by Ryan Byrne

TypeError: expected str, bytes or os.PathLike object, not tuple #

The Python "TypeError: expected str, bytes or os.PathLike object, not tuple" occurs when we pass a tuple instead of a string when opening a file. To solve the error, use a for loop if you have to open multiple files or use the addition operator to get a filename from multiple strings.

typeerror expected str bytes or os pathlike object not tuple

Here is an example of how the error occurs.

main.py
# 👇️ tuple filenames = 'example-1.txt', 'example-2.txt' # ⛔️ TypeError: expected str, bytes or os.PathLike object, not tuple with open(filenames, 'r', encoding='utf-8') as f: lines = f.readlines() for line in lines: print(line)

The open() function expects a string for its first argument but we passed it a tuple.

If you have to open multiple files, use a for loop.

main.py
# 👇️ tuple filenames = 'example-1.txt', 'example-2.txt' for filename in filenames: with open(filename, 'r', encoding='utf-8') as f: lines = f.readlines() for line in lines: print(line)

We used a for loop to iterate over the tuple and passed each filename to the open() function.

Notice that we are passing a string for the filename and not a tuple.

You might also get the error when trying to concatenate multiple strings to get the filename.

main.py
# 👇️ tuple filename = 'example', '.txt' # ⛔️ TypeError: expected str, bytes or os.PathLike object, not tuple with open(filename, 'r', encoding='utf-8') as f: lines = f.readlines() for line in lines: print(line)

The code sample creates a tuple by mistake.

One way to concatenate strings is to use the addition (+) operator.

main.py
# 👇️ this is a string filename = 'example' + '.txt' with open(filename, 'r', encoding='utf-8') as f: lines = f.readlines() for line in lines: print(line)

The addition (+) operator can be used to concatenate strings.

main.py
print('a' + 'b' + 'c') # 👉️ 'abc'

Alternatively, you can use a formatted string literal.

main.py
name = 'example' ext = 'txt' filename = f'{name}.{ext}' with open(filename, 'r', encoding='utf-8') as f: lines = f.readlines() for line in lines: print(line)
Formatted string literals (f-strings) let us include expressions inside of a string by prefixing the string with f.

Make sure to wrap expressions in curly braces - {expression}.

In case you declared a tuple by mistake, tuples are constructed in multiple ways:

  • Using a pair of parenthesis () creates an empty tuple
  • Using a trailing comma - a, or (a,)
  • Separating items with commas - a, b or (a, b)
  • Using the tuple() constructor

If you aren't sure what type a variable stores, use the built-in type() class.

main.py
my_tuple = 'example', '.txt' print(type(my_tuple)) # 👉️ <class 'tuple'> print(isinstance(my_tuple, tuple)) # 👉️ True my_str = 'example.txt' print(type(my_str)) # 👉️ <class 'str'> print(isinstance(my_str, str)) # 👉️ True

The type class returns the type of an object.

The isinstance function returns True if the passed in object is an instance or a subclass of the passed in class.

Use the search field on my Home Page to filter through my more than 1,000 articles.