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

avatar
Borislav Hadzhiev

Last updated: Feb 2, 2023
4 min

banner

# Table of Contents

  1. Expected str, bytes or os.PathLike object, not TextIOWrapper
  2. TypeError: expected str, bytes or os.PathLike object, not tuple

# Expected str, bytes or os.PathLike object, not TextIOWrapper

The Python "TypeError: expected str, bytes or os.PathLike object, not TextIOWrapper" occurs when we pass a file object instead of a string when opening a file.

To solve the error, pass the filename (as a string) to the open() function.

python typeerror expected str bytes or os pathlike object not textiowrapper

Here is an example of how the error occurs.

main.py
with open('example.txt', 'r', encoding='utf-8') as file1: lines = file1.readlines() print(lines) # โ›”๏ธ TypeError: expected str, bytes or os.PathLike object, not TextIOWrapper with open(file1, 'r', encoding='utf-8') as file2: lines = file2.readlines() print(lines)

The error is caused because we passed a file object instead of a string in the second call to the open() function.

# Pass the filename as a string to the open() function

The first argument the open() function expects is a string that represents the filename.

To solve the error, pass a string for the filename.

main.py
with open('example.txt', 'r', encoding='utf-8') as file1: lines = file1.readlines() print(lines) with open('example-2.txt', 'r', encoding='utf-8') as file2: lines = file2.readlines() print(lines)

pass filename as string to open function

The first argument the open function takes is a string.

The second is the mode (e.g. open file for reading or writing). Here are the most common modes:

NameDescription
'r'open for reading
'w'open for writing (truncates the file first)
'a'open for writing, appending to the end of the file if it exists

The third argument is the encoding - utf-8 in the example.

# Construct a filename by concatenating strings

If you need to construct the filename by concatenating strings, 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)

construct filename by concatenating strings

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}.

Alternatively, you can use the addition (+) operator.

main.py
name = 'example' ext = 'txt' filename = name + '.' + ext with open(filename, 'r', encoding='utf-8') as f: lines = f.readlines() for line in lines: print(line)

construct filename with addition operator

Make sure the values of the left and right-hand sides of the addition (+) operator are strings.

We used the with statement in the example.

The with statement takes care of automatically closing the file after we finish reading or writing.

You can also use the open() function directly, but you have to close the file manually after you finish.

main.py
file1 = open('example.txt', 'r', encoding='utf-8') lines = file1.readlines() print(lines) file1.close() # ๐Ÿ‘ˆ๏ธ have to close file

On the other hand, the with statement closes the file automatically even if an error is raised.

# 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.

# Use a for loop if you need to open multiple files

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)

use for loop to open multiple files

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.

# Another cause of the error

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}.

# How tuples are constructed in Python

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

  • Using a pair of parentheses () 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.

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