Using python to search for local file information and write it to a file

Source: Internet
Author: User
This article describes how to use python to search for local files and write information to files. It involves Python file traversal and attribute operations related skills, for more information about how to search local files and write data to files, see the example in this article. We will share this with you for your reference. The details are as follows:

Main functions:

On the specified drive letter, such as drive D, search for files related to the user's given suffix name (such as jpg and png), and then search the information (the absolute path of the relevant file ), stored in the user-specified file (if the file does not exist, create the corresponding file)

Let's take a look at the running effect first:

The first part of the running effect:

The following part of the running effect:

The file after the information is written:

Code Section:

# Search for files related to the User-Defined suffix (for example, jpg or png) on the specified drive letter, such as drive D # Then search the information (the absolute path of the related file ), import osimport time in user-specified # file (if the file does not exist, create the corresponding file) # specify the drive letter DESK = 'e: \ '# path of the information storage file ######### create this file first, during file operations, ########## use OS. mknod ('e: \ info.txt '), the system does not create the SAVE_FILE = 'e: \ info.txt' # file suffix type FILE_EXT = ['bmp ', 'jpeg ', 'gif', 'psd ', 'png ', 'jpg '] # define the global variable my_dirs = [] my_files = [] # number of files FILES_NUMBER = 0 # number of files meeting the requirements RIGHT_FILES_NUMBER = 0 # number of files NOT meeting the requirements _ RIGHT_FILES_NUMBER = 0 # Number of folders DIR_NUMBER = 0 # obtain all files and folders under the specified folder # If the specified folder does not exist, the system returns the corresponding prompt def listdir (dir_path ): if OS. path. exists (dir_path): return OS. listdir (dir_path) else: return 'Directory '+ dir_path +' nonexistent '# search file main function 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): # It is the file global FILES_NUMBER = FILES_NUMBER + 1 lists = path. split ('. ') # print ('============================================ ========= ', lists) file_ext = lists [-1] # file extension if file_ext in FILE_EXT: global 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 ('File: ', path) else: global NOT_RIGHT_FILES_NUMBER = NOT_RIGHT_FILES_NUMBER + 1 elif OS. path. isdir (path): # Is the global DIR_NUMBER = DIR_NUMBER + 1 for name in listdir (path): # print (OS. path. join (path, name) search_files (path, name) # obtain the file size def get_file_size (path): if OS. path. exists (path): return OS. path. getsize (path) # write information 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 (' file: {} does not exist! '. Format (SAVE_FILE) # Read all information def read_info (): if OS. path. exists (path): with open (SAVE_FILE, 'r + ') as fp: for line in fp: print (line) else: print (' file :{} does not exist! '. Format (SAVE_FILE) if _ name _ = '_ main _': for d in listdir (DESK): my_dirs.append (OS. path. join (DESK, d) print (my_dirs) # This is used for testing. Because many files and folders are involved in scanning the entire drive letter, it may take some time # Here you can use a folder as the test my_dir = ['e: \ test'] for path in my_dir: search_files (path ,'') print ('#' * 50) print (my_files) print ('#' * 50) print ('starts writing information... ') content = ''. join (my_files) write_info (content) print ('#' * 50) print ('starts to read information... ') read_info () print (' # '* 50) print ('Total number of search folders: {0}, total number of files: {1 }'. format (DIR_NUMBER, FILES_NUMBER) print ('Total number of files meeting the requirements: {0}, total number of files not meeting the requirements: {1 }'. format (RIGHT_FILES_NUMBER, NOT_RIGHT_FILES_NUMBER ))

Running console status;

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 ', and '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(11_hongten.jpg file: E: \ test \ upload file: e: \ test \ Upload File: E: \ test \ 20130627_183059Hongten.jpg file: E: \ test \ Upload File: E: \ test \ 20130627_1831_hongten.jpg file: E: \ test \ upload file: e: \ test \ release file: E: \ test \ 20130627_183749Hongten.jpg file: E: \ test \ release file: E: \ test \ 20130627_203658Hongten.jpg file: E: \ test \ release file: e: \ test \ Upload File: E: \ test \ 20130627_20520.hongten.jpg file: E: \ test \ 20130627_205159Hongten.jpg file: E: \ test \ Upload File: E: \ test \ upload file: e: \ test \ 20130627_205315Hongten.jpg file: E: \ test \ example file: E: \ test \ 20130627_205627Hongten.jpg file: E: \ test \ example file: e: \ test \ hongten \ 6.27 \ 20130627_140132Hongten.jpg file: E: \ test \ hongten \ 6.27 \ example file: E: \ test \ hongten \ 6.27 \ example file: E: \ test \ hongten \ 6.27 \ 20130627_183054Hongten.jpg file: E: \ test \ hongten \ 6.27 \ 20130627_183059Hongten.jpg file: E: \ test \ hongten \ 6.27 \ example file: E: \ test \ hongten \ 6.27 \ 20130627_183104hongten.jpg file: E: \ test \ hongten \ 6.27 \ examples file: E: \ test \ hongten \ 6.27 \ 20130627_183714Hongten.jpg file: E: \ test \ hongten \ 6.27 \ 20130627_183749Hongten.jpg file: E: \ test \ hongten \ 6.27 \ examples file: E: \ test \ hongten \ 6.27 \ 20130627_203658Hongten.jpg file: E: \ test \ hongten \ 6.27 \ 20130627_2020.2hongten.jpg file: E: \ test \ hongten \ 6.27 \ Folder file: E: \ test \ hongten \ 6.27 \ 20130627_2051_hongten.jpg file: E: \ test \ hongten \ 6.27 \ 20130627_205159Hongten.jpg file: E: \ test \ hongten \ 6.27 \ examples file: E: \ test \ hongten \ 6.27 \ 20130627_205257Hongten.jpg file: E: \ test \ hongten \ 6.27 \ 20130627_205315Hongten.jpg file: E: \ test \ hongten \ 6.27 \ examples 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 ################################ ################# ['2017-07-28 17:14:49 E: \ test \ 20130627_140132Hongten.jpg 0 \ n', '2017-07-28 17:14:49 E: \ test \ 20130627_182913(11_hongten.jpg 2013 \ n', '2017-07-28 17:14:49 E: \ test \ 20130627_183008(1108hongten.jpg 2315326 \ n', '2017-07-28 17:14:49 E: \ test \ 20130627_183054Hongten.jpg 2013 \ n', '2017-07-28 17:14:49 E: \ test \ 20130627_183059Hongten.jpg 2006608 \ n', '2017-07-28 17:14:49 E: \ test \ 20130627_183101Hongten.jpg 2013 \ n', '2017-07-28 17:14:49 E: \ test \ 20130627_183104hongten.jpg 2687018 \ n', '2017-07-28 17:14:49 E: \ test \ 20130627_183326Hongten.jpg 2013 \ n', '2017-07-28 17:14:49 E: \ test \ 20130627_183714Hongten.jpg 2497973 \ n', '2017-07-28 17:14:49 E: \ test \ 20130627_183749Hongten.jpg 2013 \ n', '2017-07-28 17:14:49 E: \ test \ 20130627_183925Hongten.jpg 2037776 \ n', '2017-07-28 17:14:49 E: \ test \ 20130627_203658Hongten.jpg 2013 \ n', '2017-07-28 17:14:49 E: \ test \ 20130627_20%2hongten.jpg 2837396 \ n', '2017-07-28 17:14:49 E: \ test \ 20130627_20%2(1%hongten.jpg 2013 \ n', '2017-07-28 17:14:49 E: \ test \ 20130627_20520.hongten.jpg 2745724 \ n', '2017-07-28 17:14:49 E: \ test \ 20130627_205159Hongten.jpg 2013 \ n', '2017-07-28 17:14:49 E: \ test \ 20130627_205219(11_hongten.jpg 2864744 \ n', '2017-07-28 17:14:49 E: \ test \ 20130627_205257Hongten.jpg 2013 \ n', '2017-07-28 17:14:49 E: \ test \ 20130627_205315Hongten.jpg 2832393 \ n', '2017-07-28 17:14:49 E: \ test \ 20130627_205408(11_hongten.jpg 2013 \ n', '2017-07-28 17:14:49 E: \ test \ 20130627_205425Hongten.jpg 3295286 \ n', '2017-07-28 17:14:49 E: \ test \ 20130627_205627Hongten.jpg 2013 \ n', '2017-07-28 17:14:49 E: \ test \ 20130627_205629Hongten.jpg 2813522 \ n', '2017-07-28 17:14:49 E: \ test \ hongten \ 2013 \ 20130627_140132Hongten.jpg 6.27 \ n ', '2017-07-28 17:14:49 E: \ test \ hongten \ 2013 \ 20130627_182913(11_hongten.jpg 6.27 \ n', '2017-07-28 17:14:49 E: \ test \ hongten \ 6.27 \ 20130627_183008(1108hongten.jpg 2315326 \ n', '2017-07-28 17:14:49 E: \ test \ hongten \ 6.27 \ 20130627_183054Hongten.jpg 2672977 \ n', '2017-07-28 17:14:49 E: \ test \ hongten \ 6.27 \ 20130627_183059Hongten.jpg 2006608 \ n', '2017-07-28 17:14:49 E: \ test \ hongten \ 6.27 \ 20130627_183101Hongten.jpg 2076974 \ n', '2017-07-28 17:14:49 E: \ test \ hongten \ 6.27 \ 20130627_183104hongten.jpg 2687018 \ n', '2017-07-28 17:14:49 E: \ test \ hongten \ 6.27 \ 20130627_183326Hongten.jpg 1993448 \ n', '2017-07-28 17:14:49 E: \ test \ hongten \ 6.27 \ 20130627_183714Hongten.jpg 2497973 \ n', '2017-07-28 17:14:49 E: \ test \ hongten \ 6.27 \ 20130627_183749Hongten.jpg 2066327 \ n', '2017-07-28 17:14:50 E: \ test \ hongten \ 6.27 \ 20130627_183925Hongten.jpg 2037776 \ n', '2017-07-28 17:14:50 E: \ test \ hongten \ 6.27 \ 20130627_203658Hongten.jpg 3033996 \ n', '2017-07-28 17:14:50 E: \ test \ hongten \ 6.27 \ 20130627_201_2hongten.jpg 2837396 \ n', '2017-07-28 17:14:50 E: \ test \ hongten \ 6.27 \ 20130627_203162(1%hongten.jpg 2987659 \ n', '2017-07-28 17:14:50 E: \ test \ hongten \ 6.27 \ 20130627_2051_hongten.jpg 2745724 \ n', '2017-07-28 17:14:50 E: \ test \ hongten \ 6.27 \ 20130627_205159Hongten.jpg 2824810 \ n', '2017-07-28 17:14:50 E: \ test \ hongten \ 6.27 \ 20130627_205219(11_hongten.jpg 2864744 \ n', '2017-07-28 17:14:50 E: \ test \ hongten \ 6.27 \ 20130627_205257Hongten.jpg 3092656 \ n', '2017-07-28 17:14:50 E: \ test \ hongten \ 6.27 \ 20130627_205315Hongten.jpg 2832393 \ n', '2017-07-28 17:14:50 E: \ test \ hongten \ 6.27 \ 20130627_205408(11_hongten.jpg 2796261 \ n', '2017-07-28 17:14:50 E: \ test \ hongten \ 6.27 \ 20130627_205425Hongten.jpg 3295286 \ n', '2017-07-28 17:14:50 E: \ test \ hongten \ 6.27 \ 20130627_205627Hongten.jpg 2819717 \ n', '2017-07-28 17:14:50 E: \ test \ hongten \ 6.27 \ 20130627_205629Hongten.jpg 2813522 \ n'] ############################ ########################## start to write information... ######################################## ######### start to read information... 17:14:49 E: \ test \ 20130627_140132Hongten.jpg 02013-07-28 17:14:49 E: \ test \ tests 23807472013-07-28 17:14:49 E: \ test \ 20130627_183008(11_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 \ tests 20769742013-07-28 17:14:49 E: \ test \ 26870182013 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 \ 20663272013 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_20%2hongten.jpg 28373962013-07-28 17:14:49 E: \ test \ %29876592013-07-28 17:14:49 E: \ test \ 20130627_205%hongten.jpg 27457242013-07-28 17:14:49 E: \ test \ 20130627_205159Hongten.jpg 28248102013-07-28 17:14:49 E: \ test \ tests 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 \ tests 27962612013-07-28 17:14:49 E: \ test \ 20130627_205425Hongten.jpg 32952862013-07-28 17:14:49 E: \ test \ 28197172013 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(11_hongten.jpg 23807472013-07-28 17:14:49 E: \ test \ hongten \ 6.27 \ 20130627_183008(11_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_183104hongten.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_20%2hongten.jpg 28373962013-07-28 17:14:50 E: \ test \ hongten \ 6.27 \ 20130627_20%2(1%hongten.jpg 29876592013-07-28 17:14:50 E: \ test \ hongten \ 6.27 \ 20130627_205%hongten.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(11_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(1108hongten.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 ############################### ################### total number of search folders: 4. Total number of files: 50 total number of files meeting the requirements: 46. Total number of files not meeting the requirements: 4 >>>

I hope this article will help you with Python programming.

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.