[Fixed] Expected str bytes or os.pathlike object Error

Various times we get stuck onto a type of error where it states that “Expected str bytes or os.pathlike object”, along with some prohibition stating that it does not expect either TextIOWrapper, does not expect list, does not expect _io.BufferedReader, etc.

We will see the situations where this error of ” Expected str, bytes or os.pathlike object” occurs and how to get rid of that problem.

What type of error “Expected str, bytes or os.pathlike object” is?

It comes under the category of “TypeError” in Python. “TypeError” occurs when the data type provided in an operation or a function is inappropriate. This error can occur when an operation is carried out on an object that is either not supported for the operation or of the improper type.

How to get rid of “Expected str, bytes or os.PathLike object, not TextIOWrapper”?

This error occurs when we input a file object instead of a string when trying to open some file.

Given below is an example of how the error occurs:-

with open('file.txt', 'r', encoding='utf-8') as file1:
    lines_of_file = file1.readlines()
    print(lines_of_file)
with open(file1, 'r', encoding='utf-8') as file2:
    lines_of_file = file2.readlines()
    print(lines_of_file)

The below error occurs upon execution of the code:-

The "Expected str, bytes or os.PathLike object, not TextIOWrapper", error we get on passing the wrong data type to the input.
The command line shows the error

This error occurred because we passed the wrong data type to the open function arguments. The first argument must be a string to resolve the error. The first argument of open() must contain the name or the path of the file to be opened in the form of a string, as given below:-

with open('file.txt', 'r', encoding='utf-8') as file1:
    lines_of_file = file1.readlines()
    print(lines_of_file)
with open('new_file.txt', 'r', encoding='utf-8') as file2:
    lines_of_new_file = file2.readlines()
    print(lines_of_new_file)

We can also pass formatted strings to the argument of the function in the argument if open function without causing any error.

How to resolve “Expected str, bytes or os.PathLikeobject, not io.BufferedReader”?

This type of error occurs when we try to pass a pointer to the file, while opening the file, as shown in the below code:

file = open(file_path,'rb')
ftp.storbinary("STOR " + path_of_the_file, open(file, 'rb'))
file.close()

In the first line of the above code we have opened the file, and “file” acts as its pointer, while in the second line, while using ftp.storbinary we have used the same file pointer with the open function in the place of string or formatted string, thus, causing us the error “Expected str, bytes or os.PathLikeobject, not io.BufferedReader”.

The above type of error is resolved by passing the file_name as the open() function parameter.

The way to do this is shown in the below code:-

file = open(file_path,'rb')
ftp.storbinary("STOR " + path_of_the_file, open(file_path, 'rb'))
file.close()

What happens when we pass None to open()”?

It will cause an error: “Expected str, bytes or os.PathLikeobject, not None Type”.

This error occurs when we pass the “None” value to the open function.

A straightforward example of an instance when this type of error occurs is given below:-

file_name = None
with open(file_name, 'r', encoding='utf-8') as file1:
    lines = file1.readlines()
    print(lines)

In this, we have simply passed “None” to the open functions’ argument, and thus, it throws TypeError, namely “Expected str, bytes or os.PathLikeobject, not NoneType”.

Again, a string as the first argument will solve our problem.

How to resolve “Expected str, bytes or os.PathLikeobject, not Tuple”?

As we have seen in the previous examples, one can easily see what we have tried to do in this case.

This type of error occurs when we pass a tuple to the first argument of the open function.

Below is the given an example where we can get to see this error:-

files = 'file1.txt', 'file2.txt'
with open (files, encoding = 'utf8') as file_obj:
    file_contents = file_obj.read()
    print(file_contents)

In the above code, we have passed files as the first argument of the open(). The file is a tuple containing file1.txt and file2.txt.thus, it throws the error “Expected str, bytes or os.PathLikeobject, not Tuple”.

We should use a loop if we want to read multiple files, as in the below code. The only thing to remember is that the input to the open function should only be a string or formatted strings where we can use the expression.

files = 'file1.txt', 'file2.txt'
for filename in files:
    with open (filename, encoding = 'utf8') as file_obj:
        file_contents = file_obj.read()
        print(file_contents)

How to resolve “Expected str, bytes or os.PathLikeobject, not CSV.reader”?

This error occurs when we use the CSV.reader function inside of open function, which returns a CSV.reader object.

Given below is an example of how this error occurs:-

with open (csv.reader(filename), encoding = 'utf8') as file_obj:
        file_contents = file_obj.read()
        print(file_contents)

It can simply be resolved by using an actual string in the place of CSV.reader() in open().

with open (csv.reader(filename), encoding = 'utf8') as file_obj:
   file_contents = file_obj.read()
   print(file_contents)

Solving “Expected str, bytes or os.PathLikeobject, not Nonetype” with Anaconda installation

This error can occur while installation of anaconda on our personal computer.

We can simply delete the condarc file in the directory to solve this problem.

Solving “Expected str, bytes or os.PathLikeobject, not io.BytesIO”

This error occurs when we pass io.BytesIO object in place of a string, bytes, or pathlike object.

import io
b = io.BytesIO(b"abcd")
with open(b, 'wb') as f:
    f.write(b)

Pass the filename or file path with an open function to get what is required.

Used a generator object as a first argument of the open function?:-

The below code shows the error “Expected str, bytes or os.PathLikeobject, not generator”, as we have defined a generator function and given it as an argument to the open function.

def mygenerator():
    yield "File_name"
with open(mygenerator, 'r') as f:
   lines=f.read()
   print(lines)
Upon passing generator function, we get an error stating "expected str, bytes or os.PathLike object, not generator.
The error we get upon passing the generator object to the open() function.

These errors have only one solution: using a string or file path as an open argument, as we have seen multiple times before in this article.

FAQs

What things need to be kept in mind while dealing with open() functions in Python?

Most of the time, we get to see the TypeError stating “Expected str, bytes or os.PathLikeobject” while dealing with the open() function. This error can be avoided by passing only a string to the first argument of the open() function.

What error will it show if we pass a list instead of a string in the open() function?

It will show the TypeError as “Expected str, bytes or os.PathLikeobject, not list”.

What is os.PathLike?

It is an abstract base class for the path of files in our system.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments