"Cute Python" reading notes (vi)

Source: Internet
Author: User
Tags gpg

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 cmdclass 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 informationFile name, using "*.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)          "" "             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 ==  "":   &Nbsp;         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):     "     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")     print (' Scan directory structure saved to {} '). Format (cdcfile))     def cdcgrep (Cdcpath, keyword):     "      disc Information 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 the file path and open file                  Cdcfile = open (CDCPATH + CDC)                  #  read each line of the file and follow the bad                  for line in cdcfile.readlines ():                     exist = False                     #   Determine if there is a keyword in the row                      if keyword in line:                         print ( Line)                          exist = True                 if not exist:                     print (' File {} ' cannot find the keyword {} '. Format (Cdc, keyword))                           def mkdir (pathname):     "     Automatically create a nonexistent directory      '     #  remove the trailing spaces     path= Pathname.strip ()      #  remove tail  \  symbol     path= Pathname.rstrip ("\ \")     #  determine if the path exists     if not  Os.path.exists (pathname):        try:             #  Create a catalog              os.makedirs (pathname)         except filenotfounderror as reason:             print (reason)          else:            print (' The specified directory does not exist, will be created automatically for you ')             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) >find cd Specify search keywordsFor: ' CD ' to find the target file: 1.CDC file 1.CDC, the keyword CD found the target file: 11.CDC file 11.CDC, the keyword CD search subdirectory F:\back\\back1 Search subdirectories F:\back\\back1\ BACK11 Search subdirectory F:\back\\back2 Search subdirectory F:\back\\bak Locate target file: 2.CDC file 2.CDC, keyword CD (PYCDC) >q


The above code adds the "Automatically create this directory when a nonexistent save/search directory is manually specified" feature, which implements the completed version of the pycdc!

"Cute Python" reading notes (vi)

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