“Expected str, bytes or os.PathLike object, not TextIOWrapper” in Python is a common error related to the file’s operations. To fix it you can pass in it a string instead of the file handler. Let’s go into detail to see the specific steps.
How does the error “Expected str, bytes or os.PathLike object, not TextIOWrapper” in Python happen?
This error happens due to the following reason:
First, it might be because you are working on a file. If you use the command open() to read or write to a file, then you have passed the wrong type object to the first argument of the method. For example:
# This command won't raise error with open("file0","w") as file0: print ("Declare file0 as a file handler") # The following command will raise error with open(file0,"w") as file1: print ("Error because passing file0 to the first argument")
Declare file0 as a TextIOWrapper
Traceback (most recent call last):
File "main.py", line 6, in <module>
with open(file0,"w") as file1:
TypeError: expected str, bytes or os.PathLike object, not _io.TextIOWrapper
In the example below, you have a file handler with the name file0. In this case, you have used the method open() correctly because you have passed the string to its first parameter. But then, in the next file handler which is named file1, you passed a file0 variable. This is totally wrong, as the open() method expects the str type or a path-like object instead of another file handler in the first argument. If you want to solve this problem, please read the following solution.
How to solve the error?
Passing a string instead of the file handler
If you are new to working with files in Python, you may need to get accustomed to the open() function in Python as long as file handlers. We suggest you read the tutorial about this subject in this link. To avoid this error, you can provide a string in the first argument instead:
# Open learnshareit.txt and write to file
with open("learnshareit.txt","w") as myFile:
myFile.write("LearnShareIT")
Output:
LearnShareIT
The example below shows that the string which we passed to the function is “learnshareit.txt”. In fact, this is the name of the file that the function is going to open and do some tasks to it. If the current directory (which contains the Python program you are running) does not have this file with that name yet, then it will automatically be created for you. Otherwise, it will open that file to handle.
Passing os.path object
Remember that in Python, you can also pass the os.path object in the first parameter of open(). This can help you process some enhanced paths, such as concatenating them to the current directory (using the join() method) or checking if the path exists (read the documentation here).
For example:
# You have to import os library
import os
# Open learnshareit.txt in the current OS path and write to file
with open(os.path.join("learnshareit.txt"),"w") as myFile:
myFile.write("LearnShareIT")
Output:
LearnShareIT
As we have commented, this solution requires you to import the library os. It is a built-in library, so don’t worry; you don’t have to install anything. As can be seen clearly, this solution also results in the same output as the previous one.
Summary
We have discovered how to deal with the error “Expected str, bytes or os.PathLike object, not TextIOWrapper” in Python. We recommend you use the first approach when you have to open a file located in the same directory as the Python program running. Otherwise, if you have the system path of a file that is different from the current folder, then you should use the second solution instead. Apart from this tutorial, you can solve more errors in Python with our other ones.
Maybe you are interested:
- Local variable referenced before assignment in Python
- load() missing 1 required positional argument: ‘loader’
- message: ‘geckodriver’ executable needs to be in path

I’m Edward Anderson. My current job is as a programmer. I’m majoring in information technology and 5 years of programming expertise. Python, C, C++, Javascript, Java, HTML, CSS, and R are my strong suits. Let me know if you have any questions about these programming languages.
Name of the university: HCMUT
Major: CS
Programming Languages: Python, C, C++, Javascript, Java, HTML, CSS, R