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)