当前位置: 首页 > 图灵资讯 > 行业资讯> python如何判断图片路径是否存在

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

来源:图灵python
时间: 2025-05-02 10:32:59

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

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

相关推荐:Python入门教程

本来路径上只有image文件夹,执行后会自动在image下创建result文件夹。

2、打开某些图片时,可以先判断图片的路径是否存在。

ifimg=='target7.jpg':
img_path=os.path.join(dir_path,img)
ifimghdr.what(img_path):#判断是否可以打开
target_img=Image.open(img_path)
target_img.save(os.path.join(root,'trainB/{}_B.jpg'.format(count+6)))