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)))
相关推荐
七夜zippoe2 分钟前
RabbitMQ与Celery深度集成:构建高性能Python异步任务系统
分布式·python·rabbitmq·celery·amqp
Hello阿尔法3 分钟前
SCons 一款基于 Python 的自动化构建工具
python·跨平台·构建工具·scons
Pyeako9 分钟前
Opencv计算机视觉--图像边缘检测
人工智能·python·opencv·计算机视觉·sobel·canny·图像边缘检测
深蓝电商API14 分钟前
Scrapy爬取Ajax动态加载页面三种实用方法
爬虫·python·scrapy·ajax
姓刘的哦15 分钟前
推理PyTorch模型的方法
人工智能·pytorch·python
4***175418 分钟前
Python酷库之旅-第三方库Pandas(051)
开发语言·python·pandas
__如风__22 分钟前
国产海光CPU+DCU+麒麟OS上使用Docker成功部署PaddleSpeech实战
pytorch·python·paddlepaddle
何中应31 分钟前
在Coze上新建一个插件
开发语言·python·ai
rgeshfgreh36 分钟前
Conda降级Python版本全指南
python