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)))
相关推荐
yangmf204038 分钟前
APM(三):监控 Python 服务链
大数据·运维·开发语言·python·elk·elasticsearch·搜索引擎
yangmf204040 分钟前
APM(二):监控 Python 服务
大数据·python·elasticsearch·搜索引擎
CoderJia程序员甲1 小时前
GitHub 热榜项目 - 日榜(2025-11-23)
python·开源·github·mcp
AI爱好者1 小时前
WordPress保卫战:用Python分析日志并封禁恶意爬虫
python
nvd112 小时前
Gidgethub 使用指南
开发语言·python
___波子 Pro Max.2 小时前
Python模块导入详解与最佳实践
python
CNRio3 小时前
ZUC国密算法深度研究:原理、实现与应用(Python、Rust)
python·算法·rust
Python大数据分析@4 小时前
数据分析为什么常用Jupyter而不是直接使用Python脚本或Excel?
python·jupyter·数据分析
一株菌子4 小时前
10.9总结
开发语言·python
o***Z4484 小时前
Python包管理工具
开发语言·python