python - 如果用户文件夹在目录中有空格怎么办?
问题描述
我目前正在尝试训练 Yolov3 模型(wiz young)。注释文件的格式为:
index directory width height ....
我的绝对目录是:C:\User\My Name\YOLOv3_TensorFlow\misc\JPEGImages\object.jpg
它逐行读取并根据位置确定值。由于我的用户名中有空格,它会混淆Name\YOLOv3_TensorFlow\misc\JPEGImages\object.jpg
as 宽度并返回我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:]
解决方案
我建议以下内容将目录用字符串引号括起来,并将 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")
推荐阅读
- typescript - 在 Visual Studio Code 中调试 TypeScript 渲染器
- oracle - 在页面加载 oracle apex 上设置页面项目值
- java - 如何删除红色的 JFormDesigner 评估文本?
- testing - 使用 Cypress 和 Gatsby 测试该页面不是 404 页面
- swift - SwiftUI 中的单位转换
- php - Laravel 8 - Bootstap ui 无法安装
- windows - DLL 在远程内核调试中被部分遗漏
- git - 运行颤振医生吐出错误:标准错误:致命:坏对象头
- python - 增加 CPU (ncores) 的数量对多处理池有负面影响
- python - 使用拆分文件夹而不分离某些图像