为了使用tensorflow对象检测API,我试图将.txt文件转换为.xml文件。下面是我将结果写入文件的代码:
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:预期的str、字节或os.PathLike对象,而不是_io.TextIOWrapper
有人能帮帮我吗。
发布于 2020-05-11 01:56:23
您的错误可以用下面提到的示例代码来再现:
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')请让我知道,如果你面临任何其他错误,我会很高兴帮助你。
学习愉快!
https://stackoverflow.com/questions/60664969
复制相似问题