Python queries Windows files

Source: Internet
Author: User

It's okay to use Python to write a program that queries the Windows character file


ImportOSImportwinregclassFind_assign_file:"""Drive to find the main path, which is a list file_find the file name to look up File_mode the lookup form information is: ' I ' ERROR-seeking, based on the full file name entered by the user and the suffix information to find ' H ' semi-fuzzy lookup, based on user input not including suffix information file name to find ' d ' Fuzzy Lookup, according to the user input part of the file name information to find the above three modes are all based on the user name to find, use should be associated with the following mode= ' F ' use mod E find mode ' F ' file name lookup, according to the user input file name information on the corresponding path to find a specific file ' C ' files content lookup, based on the information entered by the user to find the appropriate file"""    def __init__(Self, drive, File_find, File_mode, mode="'): Self._subkey='system\mounteddevices' Self.file_find=File_find Self.file_mode=File_mode Self.mode=Modedefgetpartitionnames (self):" "gets the system drive letter information through the registry and returns a list containing the drive letter information" "Res=[] Key=winreg. Openkey (winreg. HKEY_LOCAL_MACHINE, Self._subkey) I=0Try:             whiletrue:name, value, type=winreg. Enumvalue (key, i)ifName.startswith ('\\DosDevices\\'): Res.append ((Name, repr (value) [1:17])) I+ = 1exceptWindowserror:PassRes= [I[0][12:] +'/'  forIinchRes]returnResdefFind_begin (self, path):"""recursively traverse each folder:p Aram Path:: return:"""         forFile_pathinchPath:Try:                ifOs.path.isdir (File_path):#If the directoryList =Os.listdir (File_path)ifSelf.file_mode = ='F':                        #users find specific directoriesself.find_input (list, file_path)ifLen (list)! = 0:#If the directory is not emptyList = [File_path +'/'+ j forJinchList]#Recursiveself.find_begin (list)Else:                        #print (' {}---directory is empty '. Format (file_path))                        Pass                Else:                    #the user chooses to find a specific file based on the contents of the file                    ifSelf.file_mode = ='C': With open (File_path,'R', encoding='Utf-8', errors='Ignore') as ()ifSelf.file_findinchcontent:Print(File_path)Else:                        Pass            exceptNotadirectoryerror:#print (' {}---Invalid '. Format (file_path))                Pass            exceptPermissionerror:#print (' {}---Deny access '. Format (file_path))                Pass    deffind_input (self, List, Old_file_path):"""according to the user's chosen mode for the corresponding operation:p Aram list::p Aram Old_file_path:: Return:"""         forfile_nameinchlist:#find files based on full filename plus suffix, output file absolute path            ifSelf.mode = ='I':                iffile_name = =Self.file_find:Print('---', Old_file_path +'/'+file_name)#Search by full file name, absolute path to output files            elifSelf.mode = ='h':                ifFile_name.split ('.') [0] = =Self.file_find:Print('---', Old_file_path +'/'+file_name)#The absolute path of the output file is found based on incomplete file name            elifSelf.mode = ='D':                ifSelf.file_findinchfile_name:Print('---', Old_file_path +'/'+file_name)defGet_every_file (self):"""1. Determine whether to enter the path for the user 2. If you use a path entered by the user, the execution function 3 is called directly. If the user does not enter, the default is the system all drive letter""" = ="': . Getpartitionnames ()Else:[]#parameter is a list that contains path informationSelf.find_begin ( (): Drive= Input ("the file master path you are looking for [' path '] does not enter to query all drive characters:") File_find= Input ("Please enter the name of the file or part of the file:") File_mode= Input ("To find a file, enter the F-Find content file, enter C:")    ifFile_mode = ='F': Mode= Input ("mode (i: Full file name and suffix) (h: Full file name) (d: Partial file name):") path=find_assign_file (drive, File_find, File_mode, mode)Else: Path=find_assign_file (drive, File_find, File_mode) path.get_every_file ()if __name__=="__main__": Run ()

Some instructions for use:

1. Find the file name using the full file name and suffix

2. Use the full file name

3. Use partial file name information

4. Find file name information through the contents of the files

There are more than a few methods

Python queries Windows files

Related Article

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: 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.