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)))
相关推荐
森屿~~15 分钟前
AI 手势识别系统:踩坑与实现全记录 (PyTorch + MediaPipe)
人工智能·pytorch·python
忧郁的橙子.1 小时前
26期_01_Pyhton文件的操作
开发语言·python
小CC吃豆子2 小时前
Python爬虫
开发语言·python
June bug2 小时前
(#字符串处理)字符串中第一个不重复的字母
python·leetcode·面试·职场和发展·跳槽
lixzest3 小时前
PyTorch基础知识简述
人工智能·pytorch·python
飞Link3 小时前
深度学习里程碑:ResNet(残差网络)从理论到实战全解析
人工智能·python·深度学习
ASS-ASH3 小时前
霸王色霸气的本质概括分析
人工智能·python·机器学习·大脑·脑电波
ValidationExpression4 小时前
学习:词嵌入(Word Embedding / Text Embedding)技术
python·学习·ai
liliangcsdn4 小时前
如何使用lambda对python列表进行排序
开发语言·python
葱明撅腚4 小时前
seaborn绘图(下)
python·matplotlib·可视化·seaborn·图表绘制