目标:删除主文件夹下的文件以及主文件夹下的文件夹里的html文件。
文件:结果:
?
#!python3
import os
import sys
import cv2 as cv
"""设置主文件夹路径"""
path = 'E:/102_ningxia_wuzhong/FriesianCattle2017/2yizcfbkuv4352pzc32n54371r/'
if not os.path.exists(path): # 判断路径是否正确,如果没有就创建这个文件夹
os.makedirs(path)
if os.path.isdir(path):
dir_list = os.listdir(path)
print("path={}".format(path))
print("dirlist is{}".format(dir_list))
"""主文件夹下的文件夹"""
for i,dir_name in enumerate(dir_list):
"""找到并删除文件夹"""
dir2_path = os.path.join(path, dir_name)
if not os.path.isdir(dir2_path):
os.remove(dir2_path)
if os.path.isdir(dir2_path):
file = os.listdir(dir2_path)
for i,name in enumerate(file):
if 'html' in name:
path_delete = os.path.join(dir2_path,name)
os.remove(path_delete)
print('{} has been finished\n'.format(path_delete))
print("FINISHED!")
|