2025-08-13 12:53:33 +08:00
|
|
|
|
import os
|
|
|
|
|
|
import glob
|
|
|
|
|
|
|
|
|
|
|
|
def delete_json_files(folder_path, recursive=True, confirm=True):
|
|
|
|
|
|
"""
|
|
|
|
|
|
删除指定文件夹中的所有 .json 文件
|
|
|
|
|
|
:param folder_path: 要操作的文件夹路径
|
|
|
|
|
|
:param recursive: 是否递归删除子文件夹中的 .json 文件
|
|
|
|
|
|
:param confirm: 是否在删除前要求用户确认
|
|
|
|
|
|
"""
|
|
|
|
|
|
if not os.path.exists(folder_path):
|
|
|
|
|
|
print(f"❌ 路径不存在: {folder_path}")
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
if not os.path.isdir(folder_path):
|
|
|
|
|
|
print(f"❌ 路径不是文件夹: {folder_path}")
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
# 查找所有 .json 文件
|
|
|
|
|
|
pattern = os.path.join(folder_path, "**", "*.json") if recursive else os.path.join(folder_path, "*.json")
|
|
|
|
|
|
json_files = glob.glob(pattern, recursive=recursive)
|
|
|
|
|
|
|
|
|
|
|
|
if not json_files:
|
|
|
|
|
|
print(f"✅ 在 {folder_path} 中未找到任何 .json 文件")
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
print(f"🔍 找到 {len(json_files)} 个 .json 文件:")
|
|
|
|
|
|
for f in json_files:
|
|
|
|
|
|
print(f" - {f}")
|
|
|
|
|
|
|
|
|
|
|
|
if confirm:
|
|
|
|
|
|
answer = input("\n⚠️ 确认要删除这些文件吗?(y/N): ").strip().lower()
|
|
|
|
|
|
if answer not in ('y', 'yes'):
|
|
|
|
|
|
print("❌ 取消操作,未删除任何文件。")
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
# 执行删除
|
|
|
|
|
|
deleted_count = 0
|
|
|
|
|
|
for json_file in json_files:
|
|
|
|
|
|
try:
|
|
|
|
|
|
os.remove(json_file)
|
|
|
|
|
|
print(f"🗑️ 已删除: {json_file}")
|
|
|
|
|
|
deleted_count += 1
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
|
print(f"❌ 删除失败 {json_file}: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
print(f"\n🎉 删除完成!共删除 {deleted_count} 个 .json 文件。")
|
|
|
|
|
|
|
|
|
|
|
|
# ================== 用户配置区 ==================
|
2025-10-21 14:11:52 +08:00
|
|
|
|
FOLDER_PATH = "/media/hx/04e879fa-d697-4b02-ac7e-a4148876ebb0/dataset/camera_02/f4/folder_end" # ✅ 修改为你要删除 JSON 文件的文件夹路径
|
2025-08-13 12:53:33 +08:00
|
|
|
|
RECURSIVE = True # True: 删除所有子文件夹中的 .json;False: 只删除当前文件夹
|
|
|
|
|
|
CONFIRM_BEFORE_DELETE = True # 是否每次删除前确认(推荐开启)
|
|
|
|
|
|
|
|
|
|
|
|
# ================== 执行删除 ==================
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
|
# ⚠️ 请先修改 FOLDER_PATH 为你的实际路径!
|
|
|
|
|
|
if FOLDER_PATH == "your_folder_path_here":
|
|
|
|
|
|
print("❌ 请先修改 FOLDER_PATH 为你要操作的文件夹路径!")
|
|
|
|
|
|
else:
|
|
|
|
|
|
delete_json_files(FOLDER_PATH, RECURSIVE, CONFIRM_BEFORE_DELETE)
|