本文实例讲述了python实现搜索本地文件信息写入文件的方法。分享给大家供大家参考,具体如下:
主要功能:
在指定的盘符,如D盘,搜索出与用户给定后缀名(如:jpg,png)相关的文件,然后把搜索出来的信息(相关文件的绝对路径),存放到用户指定的文件(如果文件不存在,则建立相应的文件)中
先卡看运行效果吧:
运行效果的前部分:
运行效果的后部分:
写入信息后的文件:
代码部分:
#在指定的盘符,如D盘,搜索出与用户给定后缀名(如:jpg,png)相关的文件 #然后把搜索出来的信息(相关文件的绝对路径),存放到用户指定的 #文件(如果文件不存在,则建立相应的文件)中 import os import 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 win32 Type "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:test20130627_140132Hongten.jpg 文件: E:test20130627_182913(1)Hongten.jpg 文件: E:test20130627_183008(1)Hongten.jpg 文件: E:test20130627_183054Hongten.jpg 文件: E:test20130627_183059Hongten.jpg 文件: E:test20130627_183101Hongten.jpg 文件: E:test20130627_183116Hongten.jpg 文件: E:test20130627_183326Hongten.jpg 文件: E:test20130627_183714Hongten.jpg 文件: E:test20130627_183749Hongten.jpg 文件: E:test20130627_183925Hongten.jpg 文件: E:test20130627_203658Hongten.jpg 文件: E:test20130627_203802Hongten.jpg 文件: E:test20130627_205112(1)Hongten.jpg 文件: E:test20130627_205131Hongten.jpg 文件: E:test20130627_205159Hongten.jpg 文件: E:test20130627_205219(1)Hongten.jpg 文件: E:test20130627_205257Hongten.jpg 文件: E:test20130627_205315Hongten.jpg 文件: E:test20130627_205408(1)Hongten.jpg 文件: E:test20130627_205425Hongten.jpg 文件: E:test20130627_205627Hongten.jpg 文件: E:test20130627_205629Hongten.jpg 文件: E:testhongten6.2720130627_140132Hongten.jpg 文件: E:testhongten6.2720130627_182913(1)Hongten.jpg 文件: E:testhongten6.2720130627_183008(1)Hongten.jpg 文件: E:testhongten6.2720130627_183054Hongten.jpg 文件: E:testhongten6.2720130627_183059Hongten.jpg 文件: E:testhongten6.2720130627_183101Hongten.jpg 文件: E:testhongten6.2720130627_183116Hongten.jpg 文件: E:testhongten6.2720130627_183326Hongten.jpg 文件: E:testhongten6.2720130627_183714Hongten.jpg 文件: E:testhongten6.2720130627_183749Hongten.jpg 文件: E:testhongten6.2720130627_183925Hongten.jpg 文件: E:testhongten6.2720130627_203658Hongten.jpg 文件: E:testhongten6.2720130627_203802Hongten.jpg 文件: E:testhongten6.2720130627_205112(1)Hongten.jpg 文件: E:testhongten6.2720130627_205131Hongten.jpg 文件: E:testhongten6.2720130627_205159Hongten.jpg 文件: E:testhongten6.2720130627_205219(1)Hongten.jpg 文件: E:testhongten6.2720130627_205257Hongten.jpg 文件: E:testhongten6.2720130627_205315Hongten.jpg 文件: E:testhongten6.2720130627_205408(1)Hongten.jpg 文件: E:testhongten6.2720130627_205425Hongten.jpg 文件: E:testhongten6.2720130627_205627Hongten.jpg 文件: E:testhongten6.2720130627_205629Hongten.jpg ################################################## ['2013-07-28 17:14:49 E:test20130627_140132Hongten.jpg 0n', '2013-07-28 17:14:49 E:test20130627_182913(1)Hongten.jpg 2380747n', '2013-07-28 17:14:49 E:test20130627_183008(1)Hongten.jpg 2315326n', '2013-07-28 17:14:49 E:test20130627_183054Hongten.jpg 2672977n', '2013-07-28 17:14:49 E:test20130627_183059Hongten.jpg 2006608n', '2013-07-28 17:14:49 E:test20130627_183101Hongten.jpg 2076974n', '2013-07-28 17:14:49 E:test20130627_183116Hongten.jpg 2687018n', '2013-07-28 17:14:49 E:test20130627_183326Hongten.jpg 1993448n', '2013-07-28 17:14:49 E:test20130627_183714Hongten.jpg 2497973n', '2013-07-28 17:14:49 E:test20130627_183749Hongten.jpg 2066327n', '2013-07-28 17:14:49 E:test20130627_183925Hongten.jpg 2037776n', '2013-07-28 17:14:49 E:test20130627_203658Hongten.jpg 3033996n', '2013-07-28 17:14:49 E:test20130627_203802Hongten.jpg 2837396n', '2013-07-28 17:14:49 E:test20130627_205112(1)Hongten.jpg 2987659n', '2013-07-28 17:14:49 E:test20130627_205131Hongten.jpg 2745724n', '2013-07-28 17:14:49 E:test20130627_205159Hongten.jpg 2824810n', '2013-07-28 17:14:49 E:test20130627_205219(1)Hongten.jpg 2864744n', '2013-07-28 17:14:49 E:test20130627_205257Hongten.jpg 3092656n', '2013-07-28 17:14:49 E:test20130627_205315Hongten.jpg 2832393n', '2013-07-28 17:14:49 E:test20130627_205408(1)Hongten.jpg 2796261n', '2013-07-28 17:14:49 E:test20130627_205425Hongten.jpg 3295286n', '2013-07-28 17:14:49 E:test20130627_205627Hongten.jpg 2819717n', '2013-07-28 17:14:49 E:test20130627_205629Hongten.jpg 2813522n', '2013-07-28 17:14:49 E:testhongten6.2720130627_140132Hongten.jpg 2571032n', '2013-07-28 17:14:49 E:testhongten6.2720130627_182913(1)Hongten.jpg 2380747n', '2013-07-28 17:14:49 E:testhongten6.2720130627_183008(1)Hongten.jpg 2315326n', '2013-07-28 17:14:49 E:testhongten6.2720130627_183054Hongten.jpg 2672977n', '2013-07-28 17:14:49 E:testhongten6.2720130627_183059Hongten.jpg 2006608n', '2013-07-28 17:14:49 E:testhongten6.2720130627_183101Hongten.jpg 2076974n', '2013-07-28 17:14:49 E:testhongten6.2720130627_183116Hongten.jpg 2687018n', '2013-07-28 17:14:49 E:testhongten6.2720130627_183326Hongten.jpg 1993448n', '2013-07-28 17:14:49 E:testhongten6.2720130627_183714Hongten.jpg 2497973n', '2013-07-28 17:14:49 E:testhongten6.2720130627_183749Hongten.jpg 2066327n', '2013-07-28 17:14:50 E:testhongten6.2720130627_183925Hongten.jpg 2037776n', '2013-07-28 17:14:50 E:testhongten6.2720130627_203658Hongten.jpg 3033996n', '2013-07-28 17:14:50 E:testhongten6.2720130627_203802Hongten.jpg 2837396n', '2013-07-28 17:14:50 E:testhongten6.2720130627_205112(1)Hongten.jpg 2987659n', '2013-07-28 17:14:50 E:testhongten6.2720130627_205131Hongten.jpg 2745724n', '2013-07-28 17:14:50 E:testhongten6.2720130627_205159Hongten.jpg 2824810n', '2013-07-28 17:14:50 E:testhongten6.2720130627_205219(1)Hongten.jpg 2864744n', '2013-07-28 17:14:50 E:testhongten6.2720130627_205257Hongten.jpg 3092656n', '2013-07-28 17:14:50 E:testhongten6.2720130627_205315Hongten.jpg 2832393n', '2013-07-28 17:14:50 E:testhongten6.2720130627_205408(1)Hongten.jpg 2796261n', '2013-07-28 17:14:50 E:testhongten6.2720130627_205425Hongten.jpg 3295286n', '2013-07-28 17:14:50 E:testhongten6.2720130627_205627Hongten.jpg 2819717n', '2013-07-28 17:14:50 E:testhongten6.2720130627_205629Hongten.jpg 2813522n'] ################################################## 开始写入信息... ################################################## 开始读取信息... 2013-07-28 17:14:49 E:test20130627_140132Hongten.jpg 0 2013-07-28 17:14:49 E:test20130627_182913(1)Hongten.jpg 2380747 2013-07-28 17:14:49 E:test20130627_183008(1)Hongten.jpg 2315326 2013-07-28 17:14:49 E:test20130627_183054Hongten.jpg 2672977 2013-07-28 17:14:49 E:test20130627_183059Hongten.jpg 2006608 2013-07-28 17:14:49 E:test20130627_183101Hongten.jpg 2076974 2013-07-28 17:14:49 E:test20130627_183116Hongten.jpg 2687018 2013-07-28 17:14:49 E:test20130627_183326Hongten.jpg 1993448 2013-07-28 17:14:49 E:test20130627_183714Hongten.jpg 2497973 2013-07-28 17:14:49 E:test20130627_183749Hongten.jpg 2066327 2013-07-28 17:14:49 E:test20130627_183925Hongten.jpg 2037776 2013-07-28 17:14:49 E:test20130627_203658Hongten.jpg 3033996 2013-07-28 17:14:49 E:test20130627_203802Hongten.jpg 2837396 2013-07-28 17:14:49 E:test20130627_205112(1)Hongten.jpg 2987659 2013-07-28 17:14:49 E:test20130627_205131Hongten.jpg 2745724 2013-07-28 17:14:49 E:test20130627_205159Hongten.jpg 2824810 2013-07-28 17:14:49 E:test20130627_205219(1)Hongten.jpg 2864744 2013-07-28 17:14:49 E:test20130627_205257Hongten.jpg 3092656 2013-07-28 17:14:49 E:test20130627_205315Hongten.jpg 2832393 2013-07-28 17:14:49 E:test20130627_205408(1)Hongten.jpg 2796261 2013-07-28 17:14:49 E:test20130627_205425Hongten.jpg 3295286 2013-07-28 17:14:49 E:test20130627_205627Hongten.jpg 2819717 2013-07-28 17:14:49 E:test20130627_205629Hongten.jpg 2813522 2013-07-28 17:14:49 E:testhongten6.2720130627_140132Hongten.jpg 2571032 2013-07-28 17:14:49 E:testhongten6.2720130627_182913(1)Hongten.jpg 2380747 2013-07-28 17:14:49 E:testhongten6.2720130627_183008(1)Hongten.jpg 2315326 2013-07-28 17:14:49 E:testhongten6.2720130627_183054Hongten.jpg 2672977 2013-07-28 17:14:49 E:testhongten6.2720130627_183059Hongten.jpg 2006608 2013-07-28 17:14:49 E:testhongten6.2720130627_183101Hongten.jpg 2076974 2013-07-28 17:14:49 E:testhongten6.2720130627_183116Hongten.jpg 2687018 2013-07-28 17:14:49 E:testhongten6.2720130627_183326Hongten.jpg 1993448 2013-07-28 17:14:49 E:testhongten6.2720130627_183714Hongten.jpg 2497973 2013-07-28 17:14:49 E:testhongten6.2720130627_183749Hongten.jpg 2066327 2013-07-28 17:14:50 E:testhongten6.2720130627_183925Hongten.jpg 2037776 2013-07-28 17:14:50 E:testhongten6.2720130627_203658Hongten.jpg 3033996 2013-07-28 17:14:50 E:testhongten6.2720130627_203802Hongten.jpg 2837396 2013-07-28 17:14:50 E:testhongten6.2720130627_205112(1)Hongten.jpg 2987659 2013-07-28 17:14:50 E:testhongten6.2720130627_205131Hongten.jpg 2745724 2013-07-28 17:14:50 E:testhongten6.2720130627_205159Hongten.jpg 2824810 2013-07-28 17:14:50 E:testhongten6.2720130627_205219(1)Hongten.jpg 2864744 2013-07-28 17:14:50 E:testhongten6.2720130627_205257Hongten.jpg 3092656 2013-07-28 17:14:50 E:testhongten6.2720130627_205315Hongten.jpg 2832393 2013-07-28 17:14:50 E:testhongten6.2720130627_205408(1)Hongten.jpg 2796261 2013-07-28 17:14:50 E:testhongten6.2720130627_205425Hongten.jpg 3295286 2013-07-28 17:14:50 E:testhongten6.2720130627_205627Hongten.jpg 2819717 2013-07-28 17:14:50 E:testhongten6.2720130627_205629Hongten.jpg 2813522 ################################################## 搜索文件夹总数:4,文件总数:50 符合要求的文件总数:46,不符合要求的文件总数:4 >>>
希望本文所述对大家Python程序设计有所帮助。