python实现子文件夹中文件数目批量识别并导出条件子文件夹名单为Excel
讲解 略 代码
import os
import xlwt
def data_write(file_path, datas):
f = xlwt.Workbook()
sheet1 = f.add_sheet(u'sheet1', cell_overwrite_ok=True)
j = -1
sheet1.write(0,0,'序号')
sheet1.write(0,1,'文件名')
for data in datas:
for x in range(len(datas)):
sheet1.write(x+1, 1, datas[x])
sheet1.write(x+1, 0, j)
j += 1
f.save(file_path)
path = r"C:\Users\Lenovo\Desktop\新建文件夹"
out_floder = os.listdir(path)
error_floders = []
error_num = 0
for folders in out_floder:
folder = os.path.join(path, folders)
in_folder = os.listdir(folder)
filenum = 0
for lists in os.listdir(folder):
sub_path = os.path.join(folder, lists)
if os.path.isfile(sub_path):
filenum = filenum + 1
num = filenum
if num < 3:
error_floders.append(folders)
error_num += 1
print('姓名: ', folders)
print('文件数量: ', num)
print("共有{0}个文件格式错误,其中包括{1}".format(error_num,error_floders))
data_write(r"C:\Users\Lenovo\Desktop\test.xls", error_floders)
运行前    运行过程打印  运行结果另存为Excel 
|