预期的 str、bytes 或 os.PathLike 对象,而不是 _io.TextIOWrapper

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

我试图将 .txt 文件转换为 .xml 文件以使用 tensorflow 对象检测 API。下面是我将结果写入文件的代码:

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") 

当我 运行 执行此操作时,出现以下错误:

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

谁能帮帮我。

可以使用下面提到的示例代码重现您的错误:

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')

错误是因为 text_file = open(text_file) 行,正如 "neutrino_logic" 正确提到的那样,print(type(text_file)) 打印 <class '_io.TextIOWrapper'>.

可以通过删除行来解决错误,text_file = open(text_file)

示例工作代码如下所示:

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

如果您遇到任何其他错误,请告诉我,我很乐意为您提供帮助。

祝您学习愉快!