Last updated: Apr 8, 2024
Reading timeยท4 min
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.
Here is an example of how the error occurs.
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.
open()
functionThe first argument the open()
function expects is a string that represents the
filename.
To solve the error, pass a string for the filename.
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)
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:
Name | Description |
---|---|
'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.
If you need to construct the filename by concatenating strings, use a formatted string literal.
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)
f
.Make sure to wrap expressions in curly braces - {expression}
.
Alternatively, you can use the addition (+) operator.
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)
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.
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.
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.
Here is an example of how the error occurs.
# ๐๏ธ 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.
for
loop if you need to open multiple filesIf you have to open multiple files, use a for loop.
# ๐๏ธ 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.
# ๐๏ธ 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.
# ๐๏ธ 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.
print('a' + 'b' + 'c') # ๐๏ธ 'abc'
Alternatively, you can use a formatted string literal.
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)
f
.Make sure to wrap expressions in curly braces - {expression}
.
In case you declared a tuple by mistake, tuples are constructed in multiple ways:
()
creates an empty tuplea,
or (a,)
a, b
or (a, b)
tuple()
constructorIf you aren't sure what type a variable stores, use the built-in type()
class.
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.