[python]代码库
import os
def list_file(folder):
folder_count = 0
houzhui_num = 0
houzhui = ''
all_houzhui = []
all_houzhui_num = [] # 对应后缀名的文件数量
all_files = os.listdir(folder)
for each in all_files:
if '.' in each: # 如果此项是文件
file = os.path.splitext(each)
houzhui = file[1] # 每一个文件的后缀名,这里例如.exe
if houzhui not in all_houzhui: # 如果all_houzhui里没有.exe项
all_houzhui.append(houzhui) # 就将.exe追加进all_houzhui
houzhui_num = 1
all_houzhui_num.append(houzhui_num) #
else: # 如果all_houzhui里有.exe项
all_houzhui_num[all_houzhui.index(houzhui)] += 1 # 对应后缀名的此项数字增加1
else: # 如果此项不是文件
folder_count += 1
for each in all_houzhui:
print('文件类型为%s的文件数量为:%d' % (each,all_houzhui_num[all_houzhui.index(each)]))
print('文件夹数量:%d' % folder_count)
list_file('G:\\迅雷下载')
[代码运行效果截图]