使用默认的列表打印格式来存储和汇报实在不咋的,想着修改修改,于是:
# -*- coding: utf-8 -*- import os def 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 export cdWalker('E:\\iso\\CentOS-6.8-x86_64-bin-DVD1','newcd.cdc')
运行结果:
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 ======================================================================
其中 d 代表目录;f 代表文件。
虽然是自个儿随意设计的格式,但是小白可以体会出以下好处:
1.格式自然,可以直接人工查阅;
2.每行包含对象类型(目录或文件)所在目录和文件名;
3.搜索匹配后的输出行就是自然行,并包含必要信息;
记录的摘要文本可以再进行升级,参考标准的.ini配置文件格式,改动是不可避免的,那么有2个问题:
如何处理.ini文件
如何获得文件大小
在行者的指导下快速尝鲜一下:
>>> 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"))
查看文件try.ini
[Info] imagepath = path/to/my/cd foo = cd
>>> import os >>> os.stat("try.ini") os.stat_result(st_mode=33206, st_ino=844424930848802, st_dev=1492519019, st_nlin k=1, st_uid=0, st_gid=0, st_size=47, st_atime=1521902211, st_mtime=1521902211, s t_ctime=1521902211) >>> os.stat("try.ini").st_size 47
Bingo! 就它了!
利用基础配置处理机模块,将原先os.walk()生成的信息组织成类 ini 的文本保存下来。
# -*- coding: utf-8 -*- import os from configparser import RawConfigParser as rcp def iniCDinfo(cdrom, cdcfile): walker = {} for root, dirs, files in os.walk(cdrom): walker[root] = (dirs, files) # 根目录为字典的key,对应子目录加文件作为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')
查看newcd.ini文件内容:
[Info] imagepath = E:\iso\CentOS-6.8-x86_64-bin-DVD1 volume = newcd.cdc [Comment] 1 = E:\iso\CentOS-6.8-x86_64-bin-DVD1 2 = E:\iso\CentOS-6.8-x86_64-bin-DVD1\EFI 3 = E:\iso\CentOS-6.8-x86_64-bin-DVD1\EFI\BOOT 4 = E:\iso\CentOS-6.8-x86_64-bin-DVD1\images 5 = E:\iso\CentOS-6.8-x86_64-bin-DVD1\images\pxeboot 6 = E:\iso\CentOS-6.8-x86_64-bin-DVD1\isolinux 7 = E:\iso\CentOS-6.8-x86_64-bin-DVD1\[BOOT] [E:\iso\CentOS-6.8-x86_64-bin-DVD1] .discinfo = 31 .treeinfo = 338 centos_buildtag = 14 eula = 212 gpl = 18009 release-notes-en-us.html = 1359 rpm-gpg-key-centos-6 = 1706 rpm-gpg-key-centos-debug-6 = 1730 rpm-gpg-key-centos-security-6 = 1730 rpm-gpg-key-centos-testing-6 = 1734 trans.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 = 398 bootx64.efi = 254279 splash.xpm.gz = 1341 trans.tbl = 672 [E:\iso\CentOS-6.8-x86_64-bin-DVD1\images] efiboot.img = 380928 efidisk.img = 45373440 install.img = 146313216 trans.tbl = 888 [E:\iso\CentOS-6.8-x86_64-bin-DVD1\images\pxeboot] initrd.img = 40688737 trans.tbl = 441 vmlinuz = 4264528 [E:\iso\CentOS-6.8-x86_64-bin-DVD1\isolinux] boot.cat = 2048 boot.msg = 84 grub.conf = 321 initrd.img = 40688737 isolinux.bin = 24576 isolinux.cfg = 923 memtest = 183012 splash.jpg = 151230 trans.tbl = 2215 vesamenu.c32 = 163728 vmlinuz = 4264528 [E:\iso\CentOS-6.8-x86_64-bin-DVD1\[BOOT]] 1-boot-noemul.img = 2048 2-boot-noemul.img = 380928
[Info] 一节是软件和光盘的整体信息;[Comment] 一节是根目录列表;[目录] 各节是各个目录的文件信息
总结:通过自发的反省,对比了数据结构的优劣,不断改进,选择了最容易理解且跨平台通用的数据格式,照例在行者提醒后发觉并使用内建模块ConfigParser来进行数据文本的组织和解读。
原文:http://blog.51cto.com/9473774/2090774