Files
zjsh_yolov11/yolo11_obb/trans11.py
2025-09-11 20:44:35 +08:00

59 lines
2.2 KiB
Python

import os
import glob
def check_and_clean_unpaired_files(folder_path):
# 支持多种大小写扩展名
txt_files = {f for f in os.listdir(folder_path) if f.lower().endswith('.txt')}
jpg_files = {f for f in os.listdir(folder_path) if f.lower().endswith(('.jpg', '.jpeg'))}
# 提取不带扩展名的文件名
txt_bases = {os.path.splitext(f)[0] for f in txt_files}
jpg_bases = {os.path.splitext(f)[0] for f in jpg_files}
# 找出只有txt或只有jpg的文件名
only_txt = txt_bases - jpg_bases
only_jpg = jpg_bases - txt_bases
deleted_files = []
# 删除只有txt的文件
for base in only_txt:
for full_name in txt_files:
if os.path.splitext(full_name)[0] == base:
file_to_delete = os.path.join(folder_path, full_name)
try:
os.remove(file_to_delete)
print(f"🗑️ 删除无配对的 TXT 文件: {file_to_delete}")
deleted_files.append(file_to_delete)
except Exception as e:
print(f"❌ 删除失败 {file_to_delete}: {e}")
# 删除只有jpg的文件
for base in only_jpg:
for full_name in jpg_files:
if os.path.splitext(full_name)[0] == base:
file_to_delete = os.path.join(folder_path, full_name)
try:
os.remove(file_to_delete)
print(f"🗑️ 删除无配对的 JPG 文件: {file_to_delete}")
deleted_files.append(file_to_delete)
except Exception as e:
print(f"❌ 删除失败 {file_to_delete}: {e}")
# 统计结果
if not only_txt and not only_jpg:
print("✅ 所有文件均已配对,无需删除。")
else:
print(f"✅ 清理完成!共删除 {len(deleted_files)} 个孤立文件。")
# ============= 使用方法 =============
if __name__ == "__main__":
# 修改这里为你的目标文件夹路径
folder = r"/media/hx/04e879fa-d697-4b02-ac7e-a4148876ebb0/dataset/obb2" # ← 替换为实际路径
if os.path.isdir(folder):
check_and_clean_unpaired_files(folder)
else:
print("❌ 错误:指定的路径不是有效目录,请检查路径是否正确。")