Python implementation method for searching local file information to write to a file

Source: Internet
Author: User
The example in this article describes how Python implements the search for local file information to write to a file. Share to everyone for your reference, as follows:

Main functions:

In the specified drive letter, such as D, the search for the user given a suffix name (such as: Jpg,png) related to the file, and then the search out the information (the absolute path of the relevant file), stored in the user-specified file (if the file does not exist, then set up the corresponding file)

First card look at the effect of the operation:

Run the first part of the effect:

The latter part of the run effect:

Files After writing information:

Code section:

#在指定的盘符, such as D, search for files associated with the given suffix name (e.g., jpg,png) and store the search information (the absolute path of the relevant file) into the user-specified # file (if the file does not exist, then create the corresponding file) in import Osimport time #指定盘符DESK = ' e:\\ ' #信息保存文件的路径 ########## here, please first set up this file, I am in the process of file operation ########## use Os.mknod (' E:\\info.txt '), the system will not establish the file Save_ FILE = ' e:\\info.txt ' #文件后缀类型FILE_EXT = [' bmp ', ' jpeg ', ' gif ', ' psd ', ' png ', ' jpg '] #定义全局变量my_dirs = []my_files = []# Number of files Files_number = 0# compliant file number Right_files_number = 0# non-conforming number of files Not_right_files_number = 0# folder number Dir_number = 0# Get all files and folders under the specified folder # if the specified folder does not exist, return the appropriate message def listdir (Dir_path): If Os.path.exists (Dir_path): Return Os.listdir (dir_ Path) Else:return ' directory ' + Dir_path + ' does not exist ' #搜索文件主函数def search_files (path,name): If not Os.path.isdir (path) and not OS.P    Ath.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.STRF Time ('%y-%m-%d%h:%m:%s ', Time.localtime (Time.time ()))) size = str (get_file_size (path)) My_files.append (now+ "+ path+ ' +size+ ' \ n ') print (' File: ', 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.PA Th.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 (' files: {} does not exist!)  '. 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 (' file: {} notExist! '. Format (save_file)) if __name__ = = ' __main__ ': for D in Listdir (DESK): My_dirs.append (Os.path.join (desk,d)) print (My_ dirs) #这里是做测试用的, it may take a while to #所以这里可以使用一个文件夹作为测试 my_dir = [' e:\\test '] for the path in My_dir:search_ due to the large number of files and folders involved in scanning the entire drive letter. Files (path, ') print (' # ' *) print (' # ' * my_files) print (' # ' *) print (' Start writing info ... ') content = '. Join (My_files) Write_ Info print (' # ' *) print (' Start reading information ... ') Read_info () print (' # ' *) print (' Total number of Search Folders: {0}, Total files: {1} '. Format (dir_ Number,files_number)) Print (' Total number of files compliant: {0}, total number of files not conforming to requirements: {1} '. Format (right_files_number,not_right_files_number))

operation of the console;

Python 3.3.2 (V3.3.2:d047928ae3f6, May, 00:03:43) [MSC v.1600 + 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 '] File: E:\test\20130627_140132Hongten.jpg file: E:\test\20130627_ 182913 (1) hongten.jpg file: E:\test\20130627_183008 (1) hongten.jpg file: E:\test\20130627_183054Hongten.jpg file: E:\test\ 20130627_183059hongten.jpg file: E:\test\20130627_183101Hongten.jpg file: E:\test\20130627_183116Hongten.jpg file: E:\test \20130627_183326hongten.jpg file: E:\test\20130627_183714Hongten.jpg file: E:\test\20130627_183749Hongten.jpg file: e:\ Test\20130627_183925hongten.jpg file: E:\test\20130627_203658Hongten.jpg file: E:\teSt\20130627_203802hongten.jpg file: E:\test\20130627_205112 (1) hongten.jpg file: E:\test\20130627_205131Hongten.jpg file: E : \test\20130627_205159hongten.jpg File: E:\test\20130627_205219 (1) hongten.jpg file: E:\test\20130627_ 205257hongten.jpg file: E:\test\20130627_205315Hongten.jpg file: E:\test\20130627_205408 (1) hongten.jpg file: E:\test\ 20130627_205425hongten.jpg file: E:\test\20130627_205627Hongten.jpg file: E:\test\20130627_205629Hongten.jpg file: E:\test \hongten\6.27\20130627_140132hongten.jpg file: E:\test\hongten\6.27\20130627_182913 (1) hongten.jpg file: E:\test\ hongten\6.27\20130627_183008 (1) hongten.jpg file: E:\test\hongten\6.27\20130627_183054Hongten.jpg file: E:\test\hongten \6.27\20130627_183059hongten.jpg file: E:\test\hongten\6.27\20130627_183101Hongten.jpg file: E:\test\hongten\6.27\ 20130627_183116hongten.jpg file: E:\test\hongten\6.27\20130627_183326Hongten.jpg file: E:\test\hongten\6.27\20130627_ 183714hongten.jpg file: E:\test\hongten\6.27\20130627_183749Hongten.jpg file: E:\test\hongten\6.27\20130627_ 183925hongten.jpg File: E:\test\honGten\6.27\20130627_203658hongten.jpg file: E:\test\hongten\6.27\20130627_203802Hongten.jpg file: E:\test\hongten\6.27\ 20130627_205112 (1) hongten.jpg file: E:\test\hongten\6.27\20130627_205131Hongten.jpg file: E:\test\hongten\6.27\ 20130627_205159hongten.jpg file: E:\test\hongten\6.27\20130627_205219 (1) hongten.jpg file: E:\test\hongten\6.27\ 20130627_205257hongten.jpg file: E:\test\hongten\6.27\20130627_205315Hongten.jpg file: E:\test\hongten\6.27\20130627_ 205408 (1) hongten.jpg file: E:\test\hongten\6.27\20130627_205425Hongten.jpg file: E:\test\hongten\6.27\20130627_ 205627hongten.jpg File: 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\\2 0130627_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  : 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 ']# ################################################ #开始写入信息 ... ################################################## Start reading information ...  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, Total Files: 50 Total Files matching requirements: 46,  Total number of files that do not meet the requirements:4>>>

I hope this article is helpful for Python program design.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.