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)))
相关推荐
程序员皮皮林31 分钟前
Java 25 正式发布:更简洁、更高效、更现代!
java·开发语言·python
好家伙VCC34 分钟前
**发散创新:AI绘画编程探索与实践**随着人工智能技术的飞速发展,AI绘
java·人工智能·python·ai作画
数新网络1 小时前
Data Ingestion: Architectural Patterns
python·command-pattern
顾安r1 小时前
10.25 脚本 整理2024全年 GITHUB每周热门项目
linux·c语言·python·github·bash
新子y2 小时前
【小白笔记】 while 与 for + break 的比较分析
笔记·python
kida_yuan3 小时前
【从零开始】17. 中文摘要提取工具
python·算法·数据分析
chinesegf3 小时前
Docker篇2-用python运行项目和docker运行冲突问题
python·docker·容器
ZhengEnCi3 小时前
Python 特点和版本完全指南 - 从零基础到选择最适合的编程语言
python
鹿邑网爬4 小时前
Python 制作“满屏浪漫弹窗”教程
后端·python
小L~~~5 小时前
Python打造美观的桌面温馨提醒弹窗
开发语言·python