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)))
相关推荐
篮子里的玫瑰4 分钟前
Python与网络爬虫——列表与元组
开发语言·爬虫·python
knight_9___8 分钟前
RAG面试篇8
人工智能·python·面试·agent·rag
2301_796588509 分钟前
Go语言如何压缩文件_Go语言gzip压缩教程【基础】
jvm·数据库·python
m0_6178814211 分钟前
c++如何通过重定向rdbuf来捕获第三方库的日志输出到文件【详解】
jvm·数据库·python
Greyson120 分钟前
mysql查询执行过程中如何追踪耗时_使用PROFILE分析指令周期
jvm·数据库·python
Anesthesia丶23 分钟前
Qwen2.5-1.5b 模型部署与LORA训练笔记
pytorch·python·lora·llm·qwen·vllm
解救女汉子25 分钟前
CSS如何实现水平垂直居中效果_利用flex布局的justify-content与align-items
jvm·数据库·python
2301_7735536226 分钟前
CSS如何解决栅格重叠问题_使用Grid-area明确划分元素占位
jvm·数据库·python
覆东流30 分钟前
第6天:python综合练习——制作简易计算器
开发语言·后端·python
步辞33 分钟前
JavaScript中Symbol-keyFor检索全局符号键名逻辑
jvm·数据库·python