xml文件没有这样的文件或目录

问题描述

我已注释图像并保存在注释文件夹中。我需要将其转换为 yolov5 格式。

annotations = [os.path.join('annotations',x) for x in os.listdir('/content/gdrive/My 
Drive/annotations') if x[-3:] == "xml"]

现在注释有:

annotations/100_1_0_20170110183726390.xml
annotations/100_1_2_20170105174847679.xml
annotations/100_1_2_20170110182836729.xml

当我尝试:

for ann in tqdm(annotations):
    info_dict = extract_info_from_xml(ann)
    convert_to_yolov5(info_dict)
annotations = [os.path.join('annotations',x) for x in os.listdir('annotations') if x[-3:] == "txt"]

我得到:

FileNotFoundError: [Errno 2] No such file or directory: 
'annotations/100_1_0_20170110183726390.xml'

解决方法

您似乎正在尝试访问不存在的文件夹 annotations/100_1_0_20170110183726390.xml(虽然我想我知道您要做什么)。

程序需要知道文件的完整路径,如 /content/gdrive/My Drive/annotations/ 而不是 annotations

我认为 os.path.join('/content/gdrive/My Drive/annotations',x) for x in os.listdir('/content/gdrive/My Drive/annotations') if x[-3:] == "txt" 可能有用,虽然有点罗嗦。

如果您在与 annotations 相同的目录中编写程序,您可能可以使用 os.getcwd() 返回当前工作目录。然后你可以使用:

myPath = os.getcwd()
myPath = myPath + "annotations/"
os.path.join(myPath,x) for x in os.listdir(myPath) if x[-3:] == "txt"

我觉得。