"Cute Python" reading notes (iii)

Source: Internet
Author: User
Tags gpg

get the experience quickly and then seek theoretical support first.

Small white based on the existing experience, the software requirements of PYCDC have been further improved.

The Code of previous verification ideas becomes a handy feature that can be reused in different application environments. Little white imagine that your own PYCDC can be used like normal command-line tools.

"Can use getopt.getopt () to optimize current function functions"


# -*- coding: utf-8 -*-import osimport sysimport getoptcdrom =  ' E : \ \ ' Def cdwalker (cdrom,cdcfile):    export =  ""     for  root, dirs, files in os.walk (CDROM):         export +=  "\n %s;%s;%s"  %  (root,dirs,files)     open (Cdcfile,   ' W '). Write (export) #运行命令行工具的帮助提示     def usage ():     print (' Usage:python %s -e filename '  % sys.argv[0])     try:     opts, args = getopt.getopt (sys.argv[1:],  ' e: ')     # An input parameter will display a help prompt and exit the program     if len (opts)  == 0:         usage ()         sys.exit ()     for  opt, arg in opts:        if opt ==  '-E ':             cdwalker (Cdrom, arg)              print ("Record disc information to  %s"  % arg)          else:            usage ()              sys.exit () except getopt. Getopterror:    usage ()     sys.exit ()

Operating effect:

C:\>python Pycdc.pywUsage:python pycdc.pyw-e filenamec:\>python pycdc.pyw-e test.txt record disc information to Test.txt

View Test.txt content as follows:


E:\; [' 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:\EFI; [' BOOT ']; [' TRANS. TBL ']

E:1\efi\boot; []; [' bootx64.conf ', ' bootx64.efi ', ' splash.xpm.gz ', ' TRANS. TBL ']

E:\images; [' Pxeboot ']; [' efiboot.img ', ' efidisk.img ', ' install.img ', ' TRANS. TBL ']

E:\images\pxeboot; []; [' initrd.img ', ' TRANS. TBL ', ' Vmlinuz ']

E:\isolinux; []; [' Boot.cat ', ' boot.msg ', ' grub.conf ', ' initrd.img ', ' isolinux.bin ', ' isolinux.cfg ', ' memtest ', ' splash.jpg ', ' TRANS. TBL ', ' vesamenu.c32 ', ' Vmlinuz ']

E:\[boot]; []; [' 1-boot-noemul.img ', ' 2-boot-noemul.img ']


Small white Although not very understand the use of getopt, but also according to gourd painting scoop written out, the basic realization of the desired function.


Summary: Today we learned to use simple getopt to implement basic command-line tools, using try...except to handle exceptions, making programs easier to use

"Cute Python" reading notes (iii)

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.