文件夹和文件列表,文件夹文件列表,# -*- coding


# -*- coding:utf-8 -*-import osimport chardetfrom ConfigParser import RawConfigParser as rcpdef cdWalker(cdrom, cdcfile):    '''    光盘扫描主程序    @param cdrom: 光盘访问路径    @param cdcfile:输出的光盘信息记录文件(包含路径,绝对,相对都可以)    @return: 无, 直接输出成*.cdc文件    @attention: 从V0.7开始不使用此扫描函数,使用IniCDinfo()    '''    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):    '''    光盘信息记录格式化函数    @note: 直接使用os.walk()函数的输出信息进行重组    @param root: 当前根    @param dirs:当前根中的所有目录    @param files: 当前根中的所有文件    @return: 字串,组织好的当前目录信息    '''    export = "\n" + root + "\n"    for d in dirs:        export += "-d" + root + _smartcode(d) + "\n"    for f in files:        export += "-f %s %s \n" % (root, _smartcode(f))    export += "="*70    return exportdef iniCDinifo(cdrom, cdcfile):    '''    光盘信息.ini格式化函数    @note: 直接利用os.walk()函数的输出信息由ConfigParser.RawConfigParser           进行重组处理或.ini格式文件输出并记录    @param cdrom: 光盘访问路径    @param cdcfile; 输出的光盘信息记录文件(包含路径, 绝对,相对都可以)    @return: 无, 直接输出成组织好的类.ini的*.cdc文件    '''    walker = {}    for root, dirs, files in os.walk(cdrom):        walker[root] = (dirs, files)    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)    f = open(cdcfile, 'w')    cfg.write(f)    f.close()def _smartcode(stream):    '''    智能能匹配编码    @param stream: 需要匹配的字符串    @return: 相应编码后的字符串    '''    ustring = stream    codedetect = chardet.detect(ustring)["encoding"]    #print codedetect    try:        #print ustring        ustring = unicode(ustring, codedetect)        #print ustring        return "%s %s " % ("", ustring.encode('gb2312'))        #return "%s %s " % ("", ustring.encode('utf8'))    except:        return u"bad unicode encode try!"if __name__ =='__main__':    cdrom = "F:/Whu"    cdcfile = "F:/Whu/axiaoqiang.cdc"    cdWalker(cdrom, cdcfile)    #iniCDinifo(cdrom, cdcfile)

评论关闭