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)))
相关推荐
小明_GLC2 分钟前
Docker 构建镜像一直卡在下载 Python?
python·docker·容器
小白学大数据11 分钟前
Python爬虫实现无限滚动页面的自动点击与内容抓取
开发语言·爬虫·python·pandas
hnxaoli12 分钟前
通信小程序(九)快捷键自动改名
linux·python·小程序
lapiii35815 分钟前
[AI大模型] DAY 1 :零基础学LangChain
python·langchain·大模型·agent
阡陌..20 分钟前
pytorch模型训练使用多GPU执行报错:Bus error (core dumped)(未解决)
人工智能·pytorch·python
0思必得038 分钟前
[Web自动化] Selenium处理Cookie
前端·爬虫·python·selenium·自动化
喵手40 分钟前
Python爬虫实战:房价/租金指数时间序列爬虫实战 - 从多页采集到趋势分析的完整方案(附CSV导出 + SQLite持久化存储)!
爬虫·python·爬虫实战·零基础python爬虫教学·房价/租金指数时间序列·多页采集到趋势分析·采集结果sqlite到处
serve the people42 分钟前
python环境搭建 (六) Makefile 简单使用方法
java·服务器·python
IT北辰1 小时前
基于Vue3+python+mysql8.0的财务凭证录入系统,前后端分离完整版(可赠送源码)
python·vue
墨染青竹梦悠然1 小时前
基于Django+vue的图书借阅管理系统
前端·vue.js·后端·python·django·毕业设计·毕设