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

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

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

本来路径里只有到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)))
相关推荐
whale fall2 分钟前
celery -A tool.src.main worker --loglevel=info --queues=worker1_queue & 什么意思
python·学习·apache
naruto_lnq6 分钟前
使用Fabric自动化你的部署流程
jvm·数据库·python
喵手14 分钟前
Python爬虫实战:采集博客园 Cnblogs文章标题、发布日期、标签以及HTML正文等(附 Markdown 文档格式预览)!
爬虫·python·爬虫实战·python爬虫工程化实战·零基础python爬虫教学·博客园文章采集·博客园文章采集转md格式
OLOLOadsd12316 分钟前
柑橘类水果病害识别与分级_faster-rcnn_hrnetv2p-w32-1x_coco实现
python
我什么都学不会2 小时前
Python练习作业3
开发语言·python
板面华仔2 小时前
机器学习入门(二)——逻辑回归 (Logistic Regression)
python·机器学习
Hgfdsaqwr2 小时前
使用Flask快速搭建轻量级Web应用
jvm·数据库·python
GHZhao_GIS_RS2 小时前
python中的sort和sorted用法汇总
python·排序·列表
永恒的溪流2 小时前
环境出问题,再修改
pytorch·python·深度学习