ITPub博客

首页 > IT职业 > IT图书 > python如何判断图片路径是否存在

python如何判断图片路径是否存在

IT图书 作者:glovey 时间:2021-09-11 10:29:42 0 删除 编辑

1、在向文件夹中保存数据前,先判断该文件夹(路径)是否存在。

save_path = '/root/.../image/result'
if not os.path.exists(save_path):
    os.makedirs(save_path)

相关推荐:《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)))

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/2572/viewspace-2835936/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-08-16

  • 博文量
    86
  • 访问量
    770