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)))
相关推荐
大飞记Python8 分钟前
从“驱动地狱”到一行代码:WebDriverManager使用手记(附模板)
python·测试
Cloud_Shy61815 分钟前
Python 数据分析基础入门:《Excel Python:飞速搞定数据分析与处理》学习笔记系列(第九章 Excel 自动化 上篇)
python·数据分析·excel·numpy·pandas
子午21 分钟前
基于YOLO的玫瑰叶片检测系统~Python+深度学习+人工智能+目标检测+YOLOV8算法
人工智能·python·yolo
Hesionberger26 分钟前
LeetCode 101:对称二叉树(多语言解法)
开发语言·python
小陈的进阶之路26 分钟前
Python系列课(11)——PySpark
开发语言·python·ajax
WL_Aurora28 分钟前
备战蓝桥杯国赛【Day 12】
python·蓝桥杯
Cloud_Shy61829 分钟前
Python 数据分析基础入门:《Excel Python:飞速搞定数据分析与处理》学习笔记系列(第八章 使用读写包操作 Excel 文件 下篇)
python·数据分析·excel·numpy·pandas
tryCbest33 分钟前
Flask vs FastAPI 全方位对比与实战
python·flask·fastapi
测试员周周36 分钟前
【Appium 系列】第04节-Page Object 模式 — BasePage 基类设计
开发语言·数据库·人工智能·python·语言模型·appium·web app
无限中终36 分钟前
如何抓取某音视频的互动数据
爬虫·python