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)))
相关推荐
大模型铲屎官5 分钟前
【深度学习-Day 23】框架实战:模型训练与评估核心环节详解 (MNIST实战)
人工智能·pytorch·python·深度学习·大模型·llm·mnist
阿幸软件杂货间29 分钟前
PPT转图片拼贴工具 v1.0
python·powerpoint
AIGC_北苏41 分钟前
DrissionPage爬虫包实战分享
爬虫·python·drissionpage
YBCarry_段松啓2 小时前
uv:下一代 Python 包管理器
人工智能·python
yorushika_2 小时前
python打卡训练营打卡记录day45
开发语言·python·深度学习·tensorboard
封奚泽优2 小时前
使用Python进行函数作画
开发语言·python
fc&&fl2 小时前
大模型面试题总结
人工智能·python
databook2 小时前
稀疏表示与字典学习:让数据“瘦身”的魔法
python·机器学习·scikit-learn
Sheeep2 小时前
学习Pytest + Hypothesis——能帮你发现你自己都没想到的 bug
python·测试
站大爷IP3 小时前
用Python打造办公效率神器:从数据到文档的全流程自动化实践
python