import os import shutil def delete_subfolders(directory): # 检查目录是否存在 if not os.path.exists(directory): print("指定的目录不存在:", directory) return # 遍历目录中的所有项 for item in os.listdir(directory): item_path = os.path.join(directory, item) # 检查这个项是否是一个文件夹 if os.path.isdir(item_path): # 遍历一级子文件夹中的所有项 for subitem in os.listdir(item_path): subitem_path = os.path.join(item_path, subitem) # 如果是文件夹,则删除 if os.path.isdir(subitem_path): shutil.rmtree(subitem_path) print("已删除二级子文件夹:", subitem_path) if __name__ == "__main__": user_input = input("请输入目录地址: ") delete_subfolders(user_input)
标签:转大,MJ,item,出图,文件夹,subitem,directory,path,os From: https://www.cnblogs.com/zly324/p/17998088