Search code examples
python-3.xxmltensorflowobject-detection

expected str, bytes or os.PathLike object, not _io.TextIOWrapper


I was trying to convert a .txt file to .xml file for using tensorflow object detection API. Below is my code to write the result to a file:

with open(text_file,"w") as op:

                op.write(str(class_num))                 
                op.write(" ")
                op.write(str(x_tl))
                op.write(" ")
                op.write(str(y_tl))
                op.write(" ")
                op.write(str(x_br))
                op.write(" ")
                op.write(str(y_br))
                op.write("\n") 

When i run this, I'm getting the following error:

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

Can someone help me.


Solution

  • Your error could be reproduced with the sample code mentioned below:

    text_file = 'abc.txt'
    text_file = open(text_file)
    print(type(text_file))
    with open(text_file,"a+") as op:
      r = op.write('\n Gurudhevobhava')
    

    The error is because of the the line, text_file = open(text_file), and as "neutrino_logic" rightly mentioned, print(type(text_file)) prints <class '_io.TextIOWrapper'>.

    Error can be resolved by removing the line,text_file = open(text_file).

    Sample working code is shown below:

    text_file = 'abc.txt'
    print(type(text_file))
    with open(text_file,"a+") as op:
      r = op.write('\n Gurudhevobhava')
    

    Please let me know if you face any other error, I will be happy to help you.

    Happy Learning!