本文实例讲述了python实现搜索本地文件信息写入文件的方法。分享给大家供大家参考,具体如下:
主要功能:
在指定的盘符,如D盘,搜索出与用户给定后缀名(如:jpg,png)相关的文件,然后把搜索出来的信息(相关文件的绝对路径),存放到用户指定的文件(如果文件不存在,则建立相应的文件)中
先卡看运行效果吧:
运行效果的前部分:
运行效果的后部分:
写入信息后的文件:
代码部分:
#在指定的盘符,如D盘,搜索出与用户给定后缀名(如:jpg,png)相关的文件#然后把搜索出来的信息(相关文件的绝对路径),存放到用户指定的#文件(如果文件不存在,则建立相应的文件)中import osimport time#指定盘符DESK = 'E://'#信息保存文件的路径########## 这里请先建立好此文件,我在做文件操作的过程中########## 使用os.mknod('E://info.txt'),系统不会建立文件的SAVE_FILE = 'E://info.txt'#文件后缀类型FILE_EXT = ['bmp','jpeg','gif','psd','png','jpg']#定义全局变量my_dirs = []my_files = []#文件个数FILES_NUMBER = 0#符合要求的文件个数RIGHT_FILES_NUMBER = 0#不符合要求的文件个数NOT_RIGHT_FILES_NUMBER = 0#文件夹个数DIR_NUMBER = 0#获取指定文件夹下面的所有文件及文件夹#如果指定的文件夹不存在,则返回相应的提示信息def listdir(dir_path): if os.path.exists(dir_path): return os.listdir(dir_path) else: return '目录'+ dir_path + '不存在'#搜索文件主函数def search_files(path,name): if not os.path.isdir(path) and not os.path.isfile(path): return False path = os.path.join(path,name) if os.path.isfile(path): #是文件 global FILES_NUMBER FILES_NUMBER = FILES_NUMBER + 1 lists = path.split('.') #print('============================================',lists) file_ext = lists[-1] #文件扩展名 if file_ext in FILE_EXT: global RIGHT_FILES_NUMBER RIGHT_FILES_NUMBER = RIGHT_FILES_NUMBER + 1 global my_files now = str(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))) size = str(get_file_size(path)) my_files.append(now+' '+path+' '+size+'/n') print('文件:',path) else: global NOT_RIGHT_FILES_NUMBER NOT_RIGHT_FILES_NUMBER = NOT_RIGHT_FILES_NUMBER + 1 elif os.path.isdir(path): #是文件夹 global DIR_NUMBER DIR_NUMBER = DIR_NUMBER + 1 for name in listdir(path): #print(os.path.join(path,name)) search_files(path,name)#获取文件大小def get_file_size(path): if os.path.exists(path): return os.path.getsize(path)#写入信息def write_info(content): if os.path.exists(path): with open(SAVE_FILE,'w+') as fp: fp.write(content) fp.flush() fp.close() else: print('文件:{}不存在!'.format(SAVE_FILE))#读取所有信息def read_info(): if os.path.exists(path): with open(SAVE_FILE,'r+') as fp: for line in fp: print(line) else: print('文件:{}不存在!'.format(SAVE_FILE))if __name__ == '__main__': for d in listdir(DESK): my_dirs.append(os.path.join(DESK,d)) print(my_dirs) #这里是做测试用的,由于扫描整个盘符涉及到的文件和文件夹很多,可能要花一定的时间 #所以这里可以使用一个文件夹作为测试 my_dir = ['E://test'] for path in my_dir: search_files(path,'') print('#' * 50) print(my_files) print('#' * 50) print('开始写入信息...') content = ''.join(my_files) write_info(content) print('#' * 50) print('开始读取信息...') read_info() print('#' * 50) print('搜索文件夹总数:{0},文件总数:{1}'.format(DIR_NUMBER,FILES_NUMBER)) print('符合要求的文件总数:{0},不符合要求的文件总数:{1}'.format(RIGHT_FILES_NUMBER,NOT_RIGHT_FILES_NUMBER))
运行控制台情况;
Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32 bit (Intel)] on win32Type "copyright", "credits" or "license()" for more information.>>> ================================ RESTART ================================>>> ['E://bb', 'E://devlopment', 'E://game', 'E://hongten_download', 'E://info.txt', 'E://log4j', 'E://mydir', 'E://oracle', 'E://oracle10.2_win32', 'E://RECYCLER', 'E://svn_checkout', 'E://System Volume Information', 'E://test', 'E://The KMPlayer', 'E://windows', 'E://work']文件: E:/test/20130627_140132Hongten.jpg文件: E:/test/20130627_182913(1)Hongten.jpg文件: E:/test/20130627_183008(1)Hongten.jpg文件: E:/test/20130627_183054Hongten.jpg文件: E:/test/20130627_183059Hongten.jpg文件: E:/test/20130627_183101Hongten.jpg文件: E:/test/20130627_183116Hongten.jpg文件: E:/test/20130627_183326Hongten.jpg文件: E:/test/20130627_183714Hongten.jpg文件: E:/test/20130627_183749Hongten.jpg文件: E:/test/20130627_183925Hongten.jpg文件: E:/test/20130627_203658Hongten.jpg文件: E:/test/20130627_203802Hongten.jpg文件: E:/test/20130627_205112(1)Hongten.jpg文件: E:/test/20130627_205131Hongten.jpg文件: E:/test/20130627_205159Hongten.jpg文件: E:/test/20130627_205219(1)Hongten.jpg文件: E:/test/20130627_205257Hongten.jpg文件: E:/test/20130627_205315Hongten.jpg文件: E:/test/20130627_205408(1)Hongten.jpg文件: E:/test/20130627_205425Hongten.jpg文件: E:/test/20130627_205627Hongten.jpg文件: E:/test/20130627_205629Hongten.jpg文件: E:/test/hongten/6.27/20130627_140132Hongten.jpg文件: E:/test/hongten/6.27/20130627_182913(1)Hongten.jpg文件: E:/test/hongten/6.27/20130627_183008(1)Hongten.jpg文件: E:/test/hongten/6.27/20130627_183054Hongten.jpg文件: E:/test/hongten/6.27/20130627_183059Hongten.jpg文件: E:/test/hongten/6.27/20130627_183101Hongten.jpg文件: E:/test/hongten/6.27/20130627_183116Hongten.jpg文件: E:/test/hongten/6.27/20130627_183326Hongten.jpg文件: E:/test/hongten/6.27/20130627_183714Hongten.jpg文件: E:/test/hongten/6.27/20130627_183749Hongten.jpg文件: E:/test/hongten/6.27/20130627_183925Hongten.jpg文件: E:/test/hongten/6.27/20130627_203658Hongten.jpg文件: E:/test/hongten/6.27/20130627_203802Hongten.jpg文件: E:/test/hongten/6.27/20130627_205112(1)Hongten.jpg文件: E:/test/hongten/6.27/20130627_205131Hongten.jpg文件: E:/test/hongten/6.27/20130627_205159Hongten.jpg文件: E:/test/hongten/6.27/20130627_205219(1)Hongten.jpg文件: E:/test/hongten/6.27/20130627_205257Hongten.jpg文件: E:/test/hongten/6.27/20130627_205315Hongten.jpg文件: E:/test/hongten/6.27/20130627_205408(1)Hongten.jpg文件: E:/test/hongten/6.27/20130627_205425Hongten.jpg文件: E:/test/hongten/6.27/20130627_205627Hongten.jpg文件: E:/test/hongten/6.27/20130627_205629Hongten.jpg##################################################['2013-07-28 17:14:49 E://test//20130627_140132Hongten.jpg 0/n', '2013-07-28 17:14:49 E://test//20130627_182913(1)Hongten.jpg 2380747/n', '2013-07-28 17:14:49 E://test//20130627_183008(1)Hongten.jpg 2315326/n', '2013-07-28 17:14:49 E://test//20130627_183054Hongten.jpg 2672977/n', '2013-07-28 17:14:49 E://test//20130627_183059Hongten.jpg 2006608/n', '2013-07-28 17:14:49 E://test//20130627_183101Hongten.jpg 2076974/n', '2013-07-28 17:14:49 E://test//20130627_183116Hongten.jpg 2687018/n', '2013-07-28 17:14:49 E://test//20130627_183326Hongten.jpg 1993448/n', '2013-07-28 17:14:49 E://test//20130627_183714Hongten.jpg 2497973/n', '2013-07-28 17:14:49 E://test//20130627_183749Hongten.jpg 2066327/n', '2013-07-28 17:14:49 E://test//20130627_183925Hongten.jpg 2037776/n', '2013-07-28 17:14:49 E://test//20130627_203658Hongten.jpg 3033996/n', '2013-07-28 17:14:49 E://test//20130627_203802Hongten.jpg 2837396/n', '2013-07-28 17:14:49 E://test//20130627_205112(1)Hongten.jpg 2987659/n', '2013-07-28 17:14:49 E://test//20130627_205131Hongten.jpg 2745724/n', '2013-07-28 17:14:49 E://test//20130627_205159Hongten.jpg 2824810/n', '2013-07-28 17:14:49 E://test//20130627_205219(1)Hongten.jpg 2864744/n', '2013-07-28 17:14:49 E://test//20130627_205257Hongten.jpg 3092656/n', '2013-07-28 17:14:49 E://test//20130627_205315Hongten.jpg 2832393/n', '2013-07-28 17:14:49 E://test//20130627_205408(1)Hongten.jpg 2796261/n', '2013-07-28 17:14:49 E://test//20130627_205425Hongten.jpg 3295286/n', '2013-07-28 17:14:49 E://test//20130627_205627Hongten.jpg 2819717/n', '2013-07-28 17:14:49 E://test//20130627_205629Hongten.jpg 2813522/n', '2013-07-28 17:14:49 E://test//hongten//6.27//20130627_140132Hongten.jpg 2571032/n', '2013-07-28 17:14:49 E://test//hongten//6.27//20130627_182913(1)Hongten.jpg 2380747/n', '2013-07-28 17:14:49 E://test//hongten//6.27//20130627_183008(1)Hongten.jpg 2315326/n', '2013-07-28 17:14:49 E://test//hongten//6.27//20130627_183054Hongten.jpg 2672977/n', '2013-07-28 17:14:49 E://test//hongten//6.27//20130627_183059Hongten.jpg 2006608/n', '2013-07-28 17:14:49 E://test//hongten//6.27//20130627_183101Hongten.jpg 2076974/n', '2013-07-28 17:14:49 E://test//hongten//6.27//20130627_183116Hongten.jpg 2687018/n', '2013-07-28 17:14:49 E://test//hongten//6.27//20130627_183326Hongten.jpg 1993448/n', '2013-07-28 17:14:49 E://test//hongten//6.27//20130627_183714Hongten.jpg 2497973/n', '2013-07-28 17:14:49 E://test//hongten//6.27//20130627_183749Hongten.jpg 2066327/n', '2013-07-28 17:14:50 E://test//hongten//6.27//20130627_183925Hongten.jpg 2037776/n', '2013-07-28 17:14:50 E://test//hongten//6.27//20130627_203658Hongten.jpg 3033996/n', '2013-07-28 17:14:50 E://test//hongten//6.27//20130627_203802Hongten.jpg 2837396/n', '2013-07-28 17:14:50 E://test//hongten//6.27//20130627_205112(1)Hongten.jpg 2987659/n', '2013-07-28 17:14:50 E://test//hongten//6.27//20130627_205131Hongten.jpg 2745724/n', '2013-07-28 17:14:50 E://test//hongten//6.27//20130627_205159Hongten.jpg 2824810/n', '2013-07-28 17:14:50 E://test//hongten//6.27//20130627_205219(1)Hongten.jpg 2864744/n', '2013-07-28 17:14:50 E://test//hongten//6.27//20130627_205257Hongten.jpg 3092656/n', '2013-07-28 17:14:50 E://test//hongten//6.27//20130627_205315Hongten.jpg 2832393/n', '2013-07-28 17:14:50 E://test//hongten//6.27//20130627_205408(1)Hongten.jpg 2796261/n', '2013-07-28 17:14:50 E://test//hongten//6.27//20130627_205425Hongten.jpg 3295286/n', '2013-07-28 17:14:50 E://test//hongten//6.27//20130627_205627Hongten.jpg 2819717/n', '2013-07-28 17:14:50 E://test//hongten//6.27//20130627_205629Hongten.jpg 2813522/n']##################################################开始写入信息...##################################################开始读取信息...2013-07-28 17:14:49 E:/test/20130627_140132Hongten.jpg 02013-07-28 17:14:49 E:/test/20130627_182913(1)Hongten.jpg 23807472013-07-28 17:14:49 E:/test/20130627_183008(1)Hongten.jpg 23153262013-07-28 17:14:49 E:/test/20130627_183054Hongten.jpg 26729772013-07-28 17:14:49 E:/test/20130627_183059Hongten.jpg 20066082013-07-28 17:14:49 E:/test/20130627_183101Hongten.jpg 20769742013-07-28 17:14:49 E:/test/20130627_183116Hongten.jpg 26870182013-07-28 17:14:49 E:/test/20130627_183326Hongten.jpg 19934482013-07-28 17:14:49 E:/test/20130627_183714Hongten.jpg 24979732013-07-28 17:14:49 E:/test/20130627_183749Hongten.jpg 20663272013-07-28 17:14:49 E:/test/20130627_183925Hongten.jpg 20377762013-07-28 17:14:49 E:/test/20130627_203658Hongten.jpg 30339962013-07-28 17:14:49 E:/test/20130627_203802Hongten.jpg 28373962013-07-28 17:14:49 E:/test/20130627_205112(1)Hongten.jpg 29876592013-07-28 17:14:49 E:/test/20130627_205131Hongten.jpg 27457242013-07-28 17:14:49 E:/test/20130627_205159Hongten.jpg 28248102013-07-28 17:14:49 E:/test/20130627_205219(1)Hongten.jpg 28647442013-07-28 17:14:49 E:/test/20130627_205257Hongten.jpg 30926562013-07-28 17:14:49 E:/test/20130627_205315Hongten.jpg 28323932013-07-28 17:14:49 E:/test/20130627_205408(1)Hongten.jpg 27962612013-07-28 17:14:49 E:/test/20130627_205425Hongten.jpg 32952862013-07-28 17:14:49 E:/test/20130627_205627Hongten.jpg 28197172013-07-28 17:14:49 E:/test/20130627_205629Hongten.jpg 28135222013-07-28 17:14:49 E:/test/hongten/6.27/20130627_140132Hongten.jpg 25710322013-07-28 17:14:49 E:/test/hongten/6.27/20130627_182913(1)Hongten.jpg 23807472013-07-28 17:14:49 E:/test/hongten/6.27/20130627_183008(1)Hongten.jpg 23153262013-07-28 17:14:49 E:/test/hongten/6.27/20130627_183054Hongten.jpg 26729772013-07-28 17:14:49 E:/test/hongten/6.27/20130627_183059Hongten.jpg 20066082013-07-28 17:14:49 E:/test/hongten/6.27/20130627_183101Hongten.jpg 20769742013-07-28 17:14:49 E:/test/hongten/6.27/20130627_183116Hongten.jpg 26870182013-07-28 17:14:49 E:/test/hongten/6.27/20130627_183326Hongten.jpg 19934482013-07-28 17:14:49 E:/test/hongten/6.27/20130627_183714Hongten.jpg 24979732013-07-28 17:14:49 E:/test/hongten/6.27/20130627_183749Hongten.jpg 20663272013-07-28 17:14:50 E:/test/hongten/6.27/20130627_183925Hongten.jpg 20377762013-07-28 17:14:50 E:/test/hongten/6.27/20130627_203658Hongten.jpg 30339962013-07-28 17:14:50 E:/test/hongten/6.27/20130627_203802Hongten.jpg 28373962013-07-28 17:14:50 E:/test/hongten/6.27/20130627_205112(1)Hongten.jpg 29876592013-07-28 17:14:50 E:/test/hongten/6.27/20130627_205131Hongten.jpg 27457242013-07-28 17:14:50 E:/test/hongten/6.27/20130627_205159Hongten.jpg 28248102013-07-28 17:14:50 E:/test/hongten/6.27/20130627_205219(1)Hongten.jpg 28647442013-07-28 17:14:50 E:/test/hongten/6.27/20130627_205257Hongten.jpg 30926562013-07-28 17:14:50 E:/test/hongten/6.27/20130627_205315Hongten.jpg 28323932013-07-28 17:14:50 E:/test/hongten/6.27/20130627_205408(1)Hongten.jpg 27962612013-07-28 17:14:50 E:/test/hongten/6.27/20130627_205425Hongten.jpg 32952862013-07-28 17:14:50 E:/test/hongten/6.27/20130627_205627Hongten.jpg 28197172013-07-28 17:14:50 E:/test/hongten/6.27/20130627_205629Hongten.jpg 2813522##################################################搜索文件夹总数:4,文件总数:50符合要求的文件总数:46,不符合要求的文件总数:4>>>
希望本文所述对大家Python程序设计有所帮助。