1、在向文件夹中保存数据前,先判断该文件夹(路径)是否存在。
save_<a href="https://www.gaodaima.com/tag/path" title="查看更多关于path的文章" target="_blank">path</a> = '/root/.../image/result' if not os.path.exists(<a href="https://www.gaodaima.com/tag/save" title="查看更多关于save的文章" target="_blank">save</a>_path): os.makedirs(save_path)
www#gaodaima.com来源gaodaimacom搞#^代%!码网搞代码
相关推荐:《Python入门教程》
本来路径里只有到image文件夹的,执行完后会自动在image下创建result文件夹。
2、在打开某些图片时,可以先判断该图片的路径是否存在。
if img == 'target7.jpg': img_path = os.path.join(dir_path, img) if imghdr.what(img_path): # 判断是否能打开 target_img = Image.open(img_path) target_img.save(os.path.join(root, 'trainB/{}_B.jpg'.format(count+6)))
来源:搞代码网:原文地址:https://www.gaodaima.com