使用open()函数打开并写入文件,若文件不存在则创建
注意:文件的父目录必须存在,否则报错
这是输出xml文件的一段代码,详见https://mp.youkuaiyun.com/postedit/89343445,输出xml文件
indent = lambda x, y: ''.join([' ' for _ in range(y)]) + x
with open(os.path.join('/output', barename + '.xml'), 'w') as file:
file.write('<annotation>\n')
file.write(indent('<folder>data</folder>\n', 1))