python文件搜索,玩具系列
主要调用函数
1.os.path.getsize()
2.os.path.getctime()
3.time.strptime(),time.strftime()
4.os.walk()
# -*- coding: cp936 -*- """ ha ha search rmvb 搜寻你电脑中的电影文件 会打印出一个列表,把电影和下载电影的时间告诉你,哈哈 """ import os import time root_dit = ‘d:\\‘ file_type_list = [‘rmvb‘,‘avi‘,‘wmv‘] def file_size(addr): file_size_str = ‘‘ file_size = os.path.getsize(addr) if file_size < 1024: file_size_str = str(file_size) + ‘ B‘ if (file_size > 1024) & (file_size < 1024*1024) : file_size_str = str(file_size/1024) + ‘ KB‘ if file_size > 1024*1024: file_size_str = str(file_size/(1024*1024)) + ‘ MB‘ return file_size_str def file_time(addr): file_time = time.ctime(os.path.getctime(addr)) old = time.strptime(file_time,‘%a %b %d %H:%M:%S %Y‘) new = time.strftime(‘%Y-%m-%d %H:%M:00‘,old) return new def search_engine(root_dir,file_type_list): file_list = [] for root,dirs,files in os.walk(root_dir): for i in files: file_type = [] file_value = 0 if file_value == 0: if i[-2:] in file_type_list: file_value = 1 if file_value == 0: if i[-3:] in file_type_list: file_value = 1 if file_value == 0: if i[-4:] in file_type_list: file_value = 1 if file_value == 1: file_addr = root + ‘\\‘ + i time = file_time(file_addr) size = file_size(file_addr) file_type = file_addr,time,size print file_type[0],file_type[1],file_type[2] file_list.append(file_type) return file_list disk_list = [‘d:\\‘,‘e:\\‘,‘f:\\‘,‘g:\\‘,‘h:\\‘,‘j:\\‘,‘c:\\‘] all_list = [] for disk in disk_list: temp = search_engine(disk,file_type_list) all_list = all_list + temp print len(all_list)