With no perfect software, enough and easy-to-use software is perfect.
To sort out the code for these days today
# -*- coding: utf-8 -*-import osimport sysimport cmdfrom cdctools  IMPORT *CLASS PYCDC (cmd. CMD): def __init__ (self): # Initialize base class cmd.cmd.__init__ (self) self.CDROM = ' E:\\ISO\\CENTOS-6.8-X86_64-BIN-DVD1 ' self. cddir = ' f:\\back\\ ' # defines the prefix character of the command-line environment self.prompt = "(PYCDC) >" # defining the initial hints for the command-line environment self.intro = "" "Instructions for use: dir Directory name # specify save and search directory, default is "f:\\back\\" walk file name # Specify disc information file name, use "*.CDC" find Keywords # use to traverse all. CDC files in the Save and search directory to output lines that contain keywords ? #查询 quit #推出系统 can also use the Ctrl+d ( Unix) | CTRL + Z (dos/windows) "" " def do_walk (Self, filename): if filename == "": filename = input ("Please enter CDC file name:") print ("Specified saved file name: '%s '" % filename) cdwalker ( Self. Cdrom, self. Cddir+filename) def help_walk (self ):&Nbsp; print ("Scans the directory structure of the specified path and outputs it to the specified '. CDC ' file") def do_dir (Self, pathname): if pathname == "": pathname = input ("Please enter the specified save/Search directory (default: '%s '):" % (self. Cddir)) self. Cddir = pathname print ("Specify Save/Search directory as: '%s '" % (pathname)) mkdir (pathname) def help_dir (self): print (" Specify Save/Search directory ") def do_find (self, keyword): if keyword == "": keyword = input ("Please enter search keywords:") print ("Specify Search keyword: '%s '" % Keyword) cdcgrep (self. Cddir, keyword) def help_find (self): print ("search keywords") def do_quit (Self, arg): Sys.exit () def help_quit (self): print ("Exit program") # define a shortcut for quit do_q = do_quit def cdwalker (cdrom, cdcfile): &Nbsp; ' scans the directory structure of the specified path and saves it as a new file ' export = [] for root, dirs, files in Os.walk (CDROM): export.append ("\n %s;%s;%s" % ( root, dirs, files)) open (cdcfile, ' W '). Write (". Join (export)) print (' Scan directory structure saved to {} '. Format (cdcfile)) def cdcgrep (cdcpath, Keyword): " disc info text keyword search " # Search for files in directory filelist = os.listdir (Cdcpath) cdcpath = cdcpath + "\" # Loop file list for cdc in filelist: &nBsp; if os.path.isdir (CDCPATH+CDC): print (' Search sub-directory {} '. Format (CDCPATH+CDC)) # if a subdirectory, the recursive call completes the lookup cdcgrep (Cdcpath+cdc, keyword) else: if cdc.endswith ('. CDC '): print (' Find target file: {} '. Format (CDC)) # Flatten file paths and open files cdcfile = open (CDCPATH + CDC) # read each line of the file and follow the bad for line in cdcfile.readlines (): # Determine if there is a keyword in the row if keyword in line: print ( Line) def mkdir (pathname): " automatically create nonexistent directories ' # remove trailing spaces Path=pathname.strip () # Remove tail \ symbol path=pathname.rstrip ("\ \") # determine if the path exists if not os.path.exists (pathname): try: # Create directory os.makedirs (pathname) print (' The specified directory does not exist, will be created automatically for you ') except FileNotFoundError as reason: print (reason) if __name __ == ' __main__ ':     CDC = PYCDC () cdc.cmdloop ( )
The
works as follows:
Instruction: dir Directory name # specify save and search directory, default is "F:\ Back\ " walk file name # Specify the disc information file name, use" *.CDC " find keywords # Use to traverse all. CDC files in the Save and search directory to output lines that contain keywords ? #查询 quit #推出系统, you can also use Ctrl+d (Unix) | CTRL + Z (dos/windows) (PYCDC) >walk Enter the CDC file name: 1.CDC Specifies that the saved file name is: ' 1. CDC ' Scan directory structure saved to F:\BACK\1.CDC (PYCDC) >?dir Specify save/Search directory (PYCDC) >dir Please enter the specified save/Search directory (default: ' F:\back\ '): G:\back Specify Save/ The search directory is: ' G:\back\ ' [winerror 3] the system cannot find the path specified. : ' g:\\ ' (PYCDC) >dir f:\back\bak specifies that the save/Search directory is: ' F:\back\bak\ ' specified directory does not exist and will automatically create (PYCDC) >walk 2 for you. CDC specifies that the saved file name is: ' 2.CDC ' scan directory structure saved to F:\BACK\BAK\2.CDC (PYCDC) >find Please enter search keywords: image specifies the search keyword: ' image ' to locate the target file: 2.cdc e : \iso\centos-6.8-x86_64-BIN-DVD1; [' EFI ', ' images ', ' isolinux ', ' [BOOT] '; ['. Discinfo ', '. Treeinfo ', ' centos_buildtag ', ' EULA ', ' GPL ', ' Release-notes-en-us.html ', ' rpm-gpg-key-centos-6 ', ' rpm-gpg-key-centos-debug-6 ', ' Rpm-gpg-key-centos-security-6 ', ' rpm-gpg-key-centos-testing-6 ', ' TRANS. TBL '] e:\iso\centos-6.8-x86_64-bin-dvd1\images; [' Pxeboot ']; [' efiboot.img ', ' efidisk.img ', ' install.img ', ' TRANS. TBL '] e:\iso\centos-6.8-x86_64-bin-dvd1\images\pxeboot; []; [' initrd.img ', ' TRANS. TBL ', ' Vmlinuz ' (PYCDC) >dir f:\back Specify save/Search directory as: ' F:\back ' (PYCDC) >find Images specifies that the search keyword is: ' Images ' to locate the target file:1.cdc e:\iso\centos-6.8-x86_64-bin-dvd1; [' EFI ', ' images ', ' isolinux ', ' [BOOT] '; ['. Discinfo ', '. Treeinfo ', ' centos_buildtag ', ' EULA ', ' GPL ', ' Release-notes-en-us.html ', ' rpm-gpg-key-centos-6 ', ' rpm-gpg-key-centos-debug-6 ', ' Rpm-gpg-key-centos-security-6 ', ' rpm-gpg-Key-centos-testing-6 ', ' TRANS. TBL '] e:\iso\centos-6.8-x86_64-bin-dvd1\images; [' Pxeboot ']; [' efiboot.img ', ' efidisk.img ', ' install.img ', ' TRANS. TBL '] e:\iso\centos-6.8-x86_64-bin-dvd1\images\pxeboot; []; [' initrd.img ', ' TRANS. TBL ', ' vmlinuz ' Search subdirectories F:\back\back1 Search subdirectories F:\back\back1\back11 Search subdirectories F:\back\back2 Search subdirectories F:\back\bak find target files: 2.CDC E:\iso\CentOS-6.8-x86_64-bin-DVD1; [' EFI ', ' images ', ' isolinux ', ' [BOOT] '; ['. Discinfo ', '. Treeinfo ', ' centos_buildtag ', ' EULA ', ' GPL ', ' Release-notes-en-us.html ', ' rpm-gpg-key-centos-6 ', ' rpm-gpg-key-centos-debug-6 ', ' Rpm-gpg-key-centos-security-6 ', ' rpm-gpg-key-centos-testing-6 ', ' TRANS. TBL '] e:\iso\centos-6.8-x86_64-bin-dvd1\images; [' Pxeboot ']; [' efiboot.img ', ' efidisk.img ', ' install.img ', ' TRANS. TBL '] e:\iso\centos-6.8-x86_64-bin-dvd1\images\pxeboot; []; [' initrd.img ', ' TRANS. TBL ', ' Vmlinuz ' (PYCDC) >q
The above code is added to the "Automatically create directory when a nonexistent save/search directory is manually specified" function, the completion version of the pycdc!
"Cute Python" reading notes (vi)