首页 > 解决方案 > 如果用户文件夹在目录中有空格怎么办?

问题描述

我目前正在尝试训练 Yolov3 模型(wiz young)。注释文件的格式为:

index directory width height ....

我的绝对目录是:C:\User\My Name\YOLOv3_TensorFlow\misc\JPEGImages\object.jpg

它逐行读取并根据位置确定值。由于我的用户名中有空格,它会混淆Name\YOLOv3_TensorFlow\misc\JPEGImages\object.jpgas 宽度并返回我ValueError: invalid literal for int() with base 10: 'Name\\YOLOv3_TensorFlow\\misc\\JPEGImages\\Van_(52).jpg'

我把目录作为\(使用windows)是错误的,因为我相信原始代码是用于Linux的。另外,有什么方法可以防止空间干扰吗?

我相信这部分代码是相关的:

if 'str' not in str(type(line)):
        line = line.decode()
    s = line.strip().split(' ')
    assert len(s) > 8, 
    line_idx = int(s[0])
    pic_path = s[1]
    img_width = int(s[2])
    img_height = int(s[3])
    s = s[4:]

标签: pythondirectoryoperating-systemyolo

解决方案


我建议以下内容将目录用字符串引号括起来,并将 r 符号添加到字符串中。下面举例说明。

from PIL import Image

image = Image.open("C:\Users\ali\Pictures\spaced folder\object 1.png")

给出以下 unicode 错误: File "<stdin>", line 1 SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX

但是,在字符串引号前添加一个 r 就可以了。

image = Image.open(r"C:\Users\ali\Pictures\spaced folder\object 1.png")

推荐阅读