Traceback (most recent call last): File "c:\Users\ZhangYu\Desktop\coco\coco\xml2yolo.py", line 46, in <module> convert_xml_to_yolov5(xml_file_path, labels_path) File "c:\Users\ZhangYu\Desktop\coco\coco\xml2yolo.py", line 16, in convert_xml_to_yolov5 with open(txt_file_path, 'w') as txt_file: FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\ZhangYu\\Desktop\\coco\\VOC2007\\TXTlabels\\VOC2007/JPEGImages/000000000036.txt'
时间: 2023-07-31 22:06:59 浏览: 194
浅谈Python traceback的优雅处理
抱歉,我之前的回答有误。错误是由于路径拼接错误导致的。
请尝试将以下代码段:
```python
txt_file_name = os.path.splitext(filename)[0] + '.txt'
txt_file_path = os.path.join(labels_path, txt_file_name)
```
替换为:
```python
txt_file_name = os.path.splitext(filename)[0] + '.txt'
txt_file_subdir = os.path.relpath(os.path.dirname(xml_file_path), xml_dir_path)
txt_file_subdir_path = os.path.join(labels_path, txt_file_subdir)
os.makedirs(txt_file_subdir_path, exist_ok=True)
txt_file_path = os.path.join(txt_file_subdir_path, txt_file_name)
```
这将确保在`labels_path`目录下创建与XML文件相对路径对应的子目录,并将TXT文件保存在该子目录中。
希望这次修改能解决问题。如果你还有其他问题,请随时提问。
阅读全文