网站首页 > 教程文章 正文
import os
# 文件夹路径
image_folder = input("请输入图片路径:") # 图片文件夹
text_folder = input("请输入txt路径:") # 文本文件夹
# 获取所有图片和文本文件的名字(不带扩展名)
image_files = {os.path.splitext(f)[0] for f in os.listdir(image_folder) if f.lower().endswith(('.jpg', '.png', '.jpeg'))}
text_files = {os.path.splitext(f)[0] for f in os.listdir(text_folder) if f.lower().endswith('.txt')}
# 找出不匹配的文件
images_to_delete = image_files - text_files # 没有对应 txt 的图片
texts_to_delete = text_files - image_files # 没有对应图片的 txt(如果需要删除)
# 删除多余的图片
for img in images_to_delete:
img_path = os.path.join(image_folder, img + ".jpg") # 以 .jpg 为例
if not os.path.exists(img_path): # 兼容 .png/.jpeg
img_path = os.path.join(image_folder, img + ".png")
if os.path.exists(img_path):
os.remove(img_path)
print(f"Deleted image: {img_path}")
# 删除多余的文本(可选)
for txt in texts_to_delete:
txt_path = os.path.join(text_folder, txt + ".txt")
if os.path.exists(txt_path):
os.remove(txt_path)
print(f"Deleted text file: {txt_path}")
print("清理完成!")
代码解析
1. 获取用户输入的文件夹路径
image_folder = input("请输入图片路径:") # 图片文件夹 text_folder = input("请输入txt路径:") # 文本文件夹
- input() 让用户输入 存放图片的文件夹路径 和 存放文本的文件夹路径。
- image_folder 用于存放 图片(.jpg、.png、.jpeg)。
- text_folder 用于存放 文本文件(.txt)。
2. 获取文件夹中的文件名(不含扩展名)
image_files = {os.path.splitext(f)[0] for f in os.listdir(image_folder) if f.lower().endswith(('.jpg', '.png', '.jpeg'))} text_files = {os.path.splitext(f)[0] for f in os.listdir(text_folder) if f.lower().endswith('.txt')}
- os.listdir(image_folder) 获取 图片文件夹 下的所有文件名。
- os.path.splitext(f)[0] 获取 文件名(去掉扩展名)。
- if f.lower().endswith(('.jpg', '.png', '.jpeg')) 只保留 图片文件。
- 以上步骤得到 image_files(图片文件名集合) 和 text_files(文本文件名集合)。
图片文件夹: image1.jpg image2.png image3.jpeg 文本文件夹: image1.txt image2.txt
得到:
image_files = {"image1", "image2", "image3"} text_files = {"image1", "image2"}
3. 找出没有匹配的文件
images_to_delete = image_files - text_files # 没有对应 txt 的图片 texts_to_delete = text_files - image_files # 没有对应图片的 txt(如果需要删除)
- image_files - text_files:找出没有对应 .txt 文件的图片。
- text_files - image_files:找出没有对应图片的 .txt 文件。
按照上面的示例:
images_to_delete = {"image3"} # 没有对应 image3.txt texts_to_delete = set() # 没有多余的 txt 文件
4. 删除没有匹配的图片
for img in images_to_delete: img_path = os.path.join(image_folder, img + ".jpg") # 以 .jpg 为例 if not os.path.exists(img_path): # 兼容 .png/.jpeg img_path = os.path.join(image_folder, img + ".png") if os.path.exists(img_path): os.remove(img_path) print(f"Deleted image: {img_path}")
- 遍历 images_to_delete,找到 没有对应文本文件的图片。
- 依次尝试 .jpg、.png 等扩展名,检查文件是否存在。
- 如果找到对应的图片文件,就 删除 并打印 "Deleted image: 路径"。
5. 删除没有匹配的文本文件
for txt in texts_to_delete: txt_path = os.path.join(text_folder, txt + ".txt") if os.path.exists(txt_path): os.remove(txt_path) print(f"Deleted text file: {txt_path}")
- 遍历 texts_to_delete,找到 没有对应图片的 .txt 文件 并删除。
- (可选) 如果不想删除多余的 .txt,可以删除 for txt in texts_to_delete: 这一部分。
6. 结束程序
print("清理完成!")
- 提示用户清理完成。
代码执行示例
文件夹内容(执行前)
/images/ image1.jpg image2.png image3.jpeg image4.jpg (无对应 txt) /texts/ image1.txt image2.txt image5.txt (无对应图片)
代码执行后
删除 image4.jpg(无对应 txt) 和 image5.txt(无对应图片)。
删除的文件列表
Deleted image: /images/image4.jpg Deleted text file: /texts/image5.txt 清理完成!
文件夹最终内容
/images/ image1.jpg image2.png image3.jpeg /texts/ image1.txt image2.txt
总结
功能
- 检查图片和文本文件是否匹配。
- 删除没有对应文本的图片。
- (可选)删除没有对应图片的文本文件。
适用场景
- 适用于 AI 训练数据清理(确保图片和标注文件匹配)。
猜你喜欢
- 2025-04-11 手把手教你开发智能备份神器,小白也能30分钟搞定!
- 2025-04-11 画像笔记23- 作业流程调度(2)(作业流程图怎么画)
- 2025-04-11 Python接口自动化之常见用例读取方法介绍
- 2025-04-11 AI办公自动化:批量合并多个Excel表格的数据并汇总
- 2025-04-11 pc端微信用户图片DAT格式解码为图片
- 2025-04-11 Python自动化脚本应用与示例(pythonui自动化脚本)
- 2025-04-11 21-02-Python-文件操作下(python文件操作方法)
- 2025-04-11 Python定时任务,三步实现自动化(python定时任务,三步实现自动化效果)
- 2025-04-11 自动下载邮箱里未阅读的发票,并用邮件标题里指定的几个字命名
- 2025-04-11 照片整理很繁琐?3个python脚本帮你快速整理照片
- 最近发表
- 标签列表
-
- location.href (44)
- document.ready (36)
- git checkout -b (34)
- 跃点数 (35)
- 阿里云镜像地址 (33)
- qt qmessagebox (36)
- md5 sha1 (32)
- mybatis plus page (35)
- semaphore 使用详解 (32)
- update from 语句 (32)
- vue @scroll (38)
- 堆栈区别 (33)
- 在线子域名爆破 (32)
- 什么是容器 (33)
- sha1 md5 (33)
- navicat导出数据 (34)
- 阿里云acp考试 (33)
- 阿里云 nacos (34)
- redhat官网下载镜像 (36)
- srs服务器 (33)
- pico开发者 (33)
- https的端口号 (34)
- vscode更改主题 (35)
- 阿里云资源池 (34)
- os.path.join (33)