"Cute Python" reading notes (iv)

Source: Internet
Author: User
Tags gpg

keep denying yourself, but stick to the original will.


Small White has implemented the "store the disc content index as a *.cdc text file on your hard disk" and obtained a command-line tool-like program that can be called from the command linepython pycdc.pyw -e  test .txtQuickly specify a file name.

Like grep, now to implement the search function, open all the required files, read each row, if there is a specified keyword in line to print output to the screen ...

Combined with existing experience, can be very simple to achieve!


# -*- coding: utf-8 -*-import osdef cdcgrep (Cdcpath, keyword):     filelist = os.listdir (Cdcpath)         #  Search for files in directory     for cdc in filelist:                   #  Loop file List          if  ". CDC"  in cdc:                  #  filter other files, just focus on .cdc             print (' Find target file: {} '. Format (CDC))              cdcfile = open (CDCPATH + CDC)         #  stitching file path and opening file             for line  in cdcfile.rEadlines ():    #  reads each line of the file and loops                  if keyword in line:              #  determine if there is a keyword in the row                      print (line)                      cdcgrep ( ' f:\\back\\ ',  ' images ')

The above code can scan the "images" line with the keyword ". CDC" in the name under "F:\back\" in the specified directory.

The results of the operation are as follows:

Locate the target file: TEST1.CDC 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 '] find the target file: TEST2.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 ']


In the grep implementation example above, the processing of subdirectories is not considered, because an error occurs if the direct Open Directory is read. The next step is to improve this code to take into account the special circumstances of subdirectories.


# -*- coding: utf-8 -*-import osdef cdcgrep (Cdcpath, keyword):     expdict = {}    filelist = os.listdir (Cdcpath)           #  Search for files in directory     cdcpath = cdcpath  +  "\"     for cdc in filelist:                    #  Loop file List          if os.path.isdir (CDCPATH+CDC):             print (CDCPATH+CDC)              cdcgrep (Cdcpath+cdc, keyword)   #  if subdirectory, recursive call complete Find          else:            if  Cdc.endswith ('. CDC '): &NBSp;               print (CDC)                  cdcfile =  Open (CDCPATH + CDC)       #  flattened file path and opens file                  for line in  Cdcfile.readlines ():   #  read each line of the file and follow the bad                       if keyword in line:             #  determine if there is a keyword in the row                           print (line)                           cdcgrep (' f:\\back\\ ',  ' images ') 

The result of the operation is as follows:

Search subdirectories F:\back\\1 Locate the target file:test1.cdc 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\\2 Locate the target file:test2.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; []; [' initrd.img ',  ' TRANS. TBL ',  ' vmlinuz '] Find the target file:test3.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 ']

This sub-directory processing, using recursive call Cdcgrep () to implement.


Summary: This section implements the search function and learns the recursive invocation of the function.

"Cute Python" reading notes (iv)

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.