学习海量文件遍历后
自己的编译器竟然不可以编译
是在notebook & AI stdio 下可执行的
!tree -L 3 ./data/
#显示tree
import zipfile
import os
def unzip_data(src_path, target_path):
#判断是否是压缩包
if not os.path.isdir(target_path):
z = zipfile.ZipFile(src_path, 'r')
z.extractall(path = target_path)
z.close()
unzip_data('data/data90405/Zebra.zip', 'data/data90405/zebra')
unzip_data('data/data19748/insects.zip', 'data/data19748/insects')
size_dict = {} #文件大小
type_dict = {} #文件数量
def get_size_type(path):
files = os.listdir(path) #获取目录下文件名
for filename in files:
temp_path = os.path.join(path, filename) #拼接完整url
if os.path.isdir(temp_path):
get_size_type(temp_path) #递归调用
elif os.path.isfile(temp_path):
type_name = os.path.splitext(temp_path)[1] #将文件名与后缀分开
if not type_name:
type_name = "None"
type_dict.setdefault(type_name, 0)
type_dict[type_name]+=1
size_dict.setdefault(type_name, 0)
size_dict[type_name]+=os.path.getsize(temp_path)
path = 'data/'
get_size_type(path)
for each_type in type_dict.keys():
print('%5s 下共有[%5s]的文件共[%5d], 占用内存[%7.2f]MB'%
(path, each_type, type_dict[each_type], size_dict[each_type]/1024**2))
print('文件总数: [%d]'%sum(type_dict.values()))
print('文件占用内存: [%7.2f]GB'%(sum(size_dict.values())/1024**3))