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)))
相关推荐
2301_800976933 分钟前
python的协程
开发语言·python
Yolanda943 分钟前
【人工智能】AI问答助手项目
人工智能·python
qq_206901394 分钟前
如何在 WordPress 中通过邮箱获取用户 ID(PHP 实现)
jvm·数据库·python
亚林瓜子16 分钟前
AWS Glue Python Shell中获取外网ip
python·tcp/ip·http·shell·aws·vpc·glue
进击的松鼠38 分钟前
从对话到动作:用 Function Calling 把 LLM 接到真实 API(含流程拆解)
python·llm·agent
Polar__Star41 分钟前
HTML函数在多GPU系统中如何调用_显卡切换机制说明【汇总】
jvm·数据库·python
2301_813599551 小时前
mysql为什么不要在索引列上做运算_mysql函数索引使用场景
jvm·数据库·python
好家伙VCC1 小时前
**发散创新:基于FFmpeg的视频编码优化实践与实战代码解析**在现代多媒体系统中,
java·python·ffmpeg·音视频
人工干智能1 小时前
科普:CountVectorizer、TF、TF-IDF,三者层层递进
python·tf-idf
qq_342295821 小时前
如何监控集群 interconnect_ping与traceroute验证心跳通畅
jvm·数据库·python