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)))
相关推荐
weixin_4410036410 分钟前
357本Python精品书籍pdf电子书下载
python·pdf
心勤则明18 分钟前
基于Spring AI Alibaba的监督者模式实践
人工智能·python·spring
源码之家27 分钟前
计算机毕业设计:Python雪球网股票数据采集与可视化系统 Flask框架 数据分析 可视化 大数据 大模型 爬虫(建议收藏)✅
大数据·python·机器学习·数据挖掘·数据分析·flask·课程设计
小白学大数据28 分钟前
企业精准数据分析双路径对比:运营商大数据与 Python 爬虫技术选型与实践
大数据·开发语言·爬虫·python·数据分析
jr-create(•̀⌄•́)37 分钟前
简单视频编辑tools
python·ffmpeg
袁袁袁袁满38 分钟前
亮数据SERP API实现搜索引擎实时数据采集
爬虫·python·网络爬虫·爬山算法
citi39 分钟前
OpenViking 本地搭建指南
开发语言·python·ai
彭于晏Yan40 分钟前
Spring Boot + WebSocket 实现单聊已读未读(四)
spring boot·python·websocket
AI玫瑰助手41 分钟前
Python基础:列表的切片与嵌套列表使用技巧
android·开发语言·python
Rnan-prince43 分钟前
Count-Min Sketch:海量数据频率统计的“轻量级计数器“
python·算法