"Cute Python" reading notes (vii)

Source: Internet
Author: User
Tags gpg

no best, only more reasonable!


Use the default list printing format to store and report the fact that it is not a very well, thinking about modifying the changes, so:

# -*- coding: utf-8 -*-import osdef cdwalker (Cdrom, cdcfile):     export =  ""     for root, dirs, files in  Os.walk (CDROM):         print (Formatcdinfo (root, dirs, files) )         export += formatcdinfo (Root, dirs, files)     open (cdcfile,  ' W '). Write (export)     def formatcdinfo ( root, dirs, files):    export =  "\ n"  + root +  "\ n"     for d in dirs:        export  +=  "-d %s %s \n"  %  (root, d)     for f  in files:        export +=  "-f %s %s \n"  %  (ROOT, F)     export +=  "=" *70    return exportcdwalker (' E:\ \iso\\centos-6.8-x86_64-bin-dvd1 ', ' NEWCD.CDC ')

Run Result:

E:\iso\centos-6.8-x86_64-bin-dvd1-d e:\iso\centos-6.8-x86_64-bin-dvd1 efi -d e:\iso\ centos-6.8-x86_64-bin-dvd1 images -d e:\iso\centos-6.8-x86_64-bin-dvd1 isolinux -d  e:\iso\centos-6.8-x86_64-bin-dvd1 [boot] -f e:\iso\centos-6.8-x86_64-bin-dvd1 . discinfo -f e:\iso\centos-6.8-x86_64-bin-dvd1 .treeinfo -f e:\iso\centos-6.8-x86_64- Bin-dvd1 centos_buildtag -f e:\iso\centos-6.8-x86_64-bin-dvd1 eula -f e:\iso\ centos-6.8-x86_64-bin-dvd1 gpl -f e:\iso\centos-6.8-x86_64-bin-dvd1  Release-notes-en-us.html -f e:\iso\centos-6.8-x86_64-bin-dvd1 rpm-gpg-key-centos-6 -f  e:\iso\centos-6.8-x86_64-bin-dvd1 rpm-gpg-key-centos-debug-6 -f e:\iso\centos-6.8-x86_64- bin-dvd1 rpm-gpg-key-centos-security-6 -f e:\iso\centos-6.8-x86_64-bin-dvd1  Rpm-gpg-key-centos-testing-6 -f e:\iso\centos-6.8-x86_64-biN-dvd1 trans. tbl ======================================================================e:\iso\centos-6.8-x86_64- bin-dvd1\efi-d e:\iso\centos-6.8-x86_64-bin-dvd1\efi boot -f e:\iso\centos-6.8-x86_64- Bin-dvd1\efi trans. tbl ======================================================================e:\iso\centos-6.8-x86_64- Bin-dvd1\efi\boot-f e:\iso\centos-6.8-x86_64-bin-dvd1\efi\boot bootx64.conf -f e:\iso\ Centos-6.8-x86_64-bin-dvd1\efi\boot bootx64.efi -f e:\iso\centos-6.8-x86_64-bin-dvd1\efi\boot  splash.xpm.gz -f e:\iso\centos-6.8-x86_64-bin-dvd1\efi\boot trans. tbl ======================================================================e:\iso\centos-6.8-x86_64- Bin-dvd1\images-d e:\iso\centos-6.8-x86_64-bin-dvd1\images pxeboot -f e:\iso\ centos-6.8-x86_64-bin-dvd1\images efiboot.img -f e:\iso\centos-6.8-x86_64-bin-dvd1\images  Efidisk.img -f e:\iso\centos-6.8-x86_64-bin-dvd1\images install.img -f e:\iso\centos-6.8-x86_64-bin-dvd1\images  TRANS. tbl ======================================================================e:\iso\centos-6.8-x86_64- Bin-dvd1\images\pxeboot-f e:\iso\centos-6.8-x86_64-bin-dvd1\images\pxeboot initrd.img -f e : \iso\centos-6.8-x86_64-bin-dvd1\images\pxeboot trans. tbl -f e:\iso\centos-6.8-x86_64-bin-dvd1\images\pxeboot vmlinuz ========================== ============================================e:\iso\centos-6.8-x86_64-bin-dvd1\isolinux-f e:\iso\ Centos-6.8-x86_64-bin-dvd1\isolinux boot.cat -f e:\iso\centos-6.8-x86_64-bin-dvd1\isolinux  boot.msg -f e:\iso\centos-6.8-x86_64-bin-dvd1\isolinux grub.conf -f e:\iso\ Centos-6.8-x86_64-bin-dvd1\isolinux initrd.img -f e:\iso\centos-6.8-x86_64-bin-dvd1\isolinux  isolinux.bin -f e:\iso\centos-6.8-x86_64-bin-dvd1\isolinux isOlinux.cfg -f e:\iso\centos-6.8-x86_64-bin-dvd1\isolinux memtest -f e:\iso\ Centos-6.8-x86_64-bin-dvd1\isolinux splash.jpg -f e:\iso\centos-6.8-x86_64-bin-dvd1\isolinux  trans. Tbl -f e:\iso\centos-6.8-x86_64-bin-dvd1\isolinux vesamenu.c32 -f e:\iso\ centos-6.8-x86_64-bin-dvd1\isolinux vmlinuz ======================================================== ==============e:\iso\centos-6.8-x86_64-bin-dvd1\[boot]-f e:\iso\centos-6.8-x86_64-bin-dvd1\[boot] 1- boot-noemul.img -f e:\iso\centos-6.8-x86_64-bin-dvd1\[boot] 2-boot-noemul.img ============ ==========================================================

where d represents the directory; F represents the file.

Although it is a freely designed format, small white can realize the following benefits:

1. Natural format, can be directly manual inspection;

2. Each row contains the directory and file name of the object type (directory or file);

3. The search matches the output line is the natural line, and contains the necessary information;


The summary text of the record can be upgraded again, referring to the standard. ini configuration file format, where changes are unavoidable, then there are 2 questions:

    1. How to handle. ini Files

    2. How to get the file size


Prompt early adopters under the guidance of a person:

>>> from Configparser import rawconfigparser as rcp>>> CFG=RCP () >>> cfg.add_section (' Info ') >>> cfg.set ("info", "ImagePath", "PATH/TO/MY/CD") >>> cfg.set ("info", "foo", "CD") >>> Cfg.write (Open ("Try.ini", "w"))

View File Try.ini

[Info]imagepath = Path/to/my/cdfoo = CD


>>> Import os>>> os.stat ("Try.ini") Os.stat_result (st_mode=33206, st_ino=844424930848802, st_dev= 1492519019, St_nlink=1, St_uid=0, St_gid=0, st_size=47, st_atime=1521902211, st_mtime=1521902211, st_ctime=1521902211 ) >>> Os.stat ("Try.ini"). St_size47

Bingo! It's over!

The basic configuration processor module is used to organize the information generated by the original Os.walk () into the text of the class INI.


# -*- coding: utf-8 -*-import osfrom configparser import  Rawconfigparser as rcpdef inicdinfo (cdrom, cdcfile):     walker =  {}    for root, dirs, files in os.walk (CDROM):         walker[root] =  (dirs, files)              #  root directory is a dictionary key, corresponding subdirectory plus file as value    cfg =  RCP ()     cfg.add_section ("Info")     cfg.add_section ("Comment")     cfg.set ("info",  "ImagePath",  cdrom)     cfg.set ("info",   "Volume",  cdcfile)     dirs = walker.keys ()     i  = 0    for d in dirs:         i += 1        cfg.set ("Comment",  str (i),  d)     for p  in walker:        cfg.add_section (P)          for f in walker[p][1]:             cfg.set (P, f, os.stat ("%s/%s"  %  (p, f)). St_size)      cfg.write (Open (cdcfile,  "w")) Inicdinfo (' e:\\iso\\centos-6.8-x86_64-bin-dvd1 ', ' Newcd.ini ')

View the contents of the Newcd.ini file:

[info]imagepath = e:\iso\centos-6.8-x86_64-bin-dvd1volume = newcd.cdc[comment]1 =  e:\iso\centos-6.8-x86_64-bin-dvd12 = e:\iso\centos-6.8-x86_64-bin-dvd1\efi3 = e:\iso \centos-6.8-x86_64-bin-dvd1\efi\boot4 = e:\iso\centos-6.8-x86_64-bin-dvd1\images5 = e:\iso \centos-6.8-x86_64-bin-dvd1\images\pxeboot6 = e:\iso\centos-6.8-x86_64-bin-dvd1\isolinux7 =  e:\iso\centos-6.8-x86_64-bin-dvd1\[boot][e:\iso\centos-6.8-x86_64-bin-dvd1].discinfo = 31. treeinfo = 338centos_buildtag = 14eula = 212gpl =  18009release-notes-en-us.html = 1359rpm-gpg-key-centos-6 = 1706rpm-gpg-key-centos-debug-6  = 1730rpm-gpg-key-centos-security-6 = 1730rpm-gpg-key-centos-testing-6 =  1734trans.tbl = 3380[e:\iso\centos-6.8-x86_64-bin-dvd1\efi]trans.tbl = 216[e:\iso\ Centos-6.8-x86_64-bin-dvd1\efi\boot]bootx64.conf =&nBsp;398bootx64.efi = 254279splash.xpm.gz = 1341trans.tbl = 672[e:\iso\ Centos-6.8-x86_64-bin-dvd1\images]efiboot.img = 380928efidisk.img = 45373440install.img  = 146313216trans.tbl = 888[e:\iso\centos-6.8-x86_64-bin-dvd1\images\pxeboot]initrd.img  = 40688737trans.tbl = 441vmlinuz = 4264528[e:\iso\centos-6.8-x86_64-bin-dvd1\ isolinux]boot.cat = 2048boot.msg = 84grub.conf = 321initrd.img =  40688737isolinux.bin = 24576isolinux.cfg = 923memtest = 183012splash.jpg =  151230trans.tbl = 2215vesamenu.c32 = 163728vmlinuz = 4264528[e:\iso\ centos-6.8-x86_64-bin-dvd1\[boot]]1-boot-noemul.img = 20482-boot-noemul.img = 380928

The [INFO] section is the overall information for the software and the CD; [Comment] section is the root directory list; [directory] sections are file information for each directory


Summary: Through spontaneous introspection, comparing the merits and demerits of the data structure, continuous improvement, the selection of the most easily understood and cross-platform common data format, the usual reminder and use the built-in module Configparser to organize and interpret the data text.

"Cute Python" reading notes (vii)

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.