List of folders and files

From , 5 Years ago, written in Python, viewed 177 times.
URL https://pastebin.vip/view/d428d070
  1. # -*- coding:utf-8 -*-
  2. import os
  3. import chardet
  4. from ConfigParser import RawConfigParser as rcp
  5. def cdWalker(cdrom, cdcfile):
  6.     '''
  7.    光盘扫描主程序
  8.    @param cdrom: 光盘访问路径
  9.    @param cdcfile:输出的光盘信息记录文件(包含路径,绝对,相对都可以)
  10.    @return: 无, 直接输出成*.cdc文件
  11.    @attention: 从V0.7开始不使用此扫描函数,使用IniCDinfo()
  12.    '''
  13.     export = ""
  14.     for root, dirs, files in os.walk(cdrom):
  15.         #print formatCDinfo(root, dirs, files)
  16.         export += formatCDinfo(root, dirs, files)
  17.     open(cdcfile, 'w').write(export)
  18.  
  19. #格式化文件信息输出
  20. def formatCDinfo(root, dirs, files):
  21.     '''
  22.    光盘信息记录格式化函数
  23.    @note: 直接使用os.walk()函数的输出信息进行重组
  24.    @param root: 当前根
  25.    @param dirs:当前根中的所有目录
  26.    @param files: 当前根中的所有文件
  27.    @return: 字串,组织好的当前目录信息
  28.    '''
  29.     export = "\n" + root + "\n"
  30.     for d in dirs:
  31.         export += "-d" + root + _smartcode(d) + "\n"
  32.     for f in files:
  33.         export += "-f %s %s \n" % (root, _smartcode(f))
  34.     export += "="*70
  35.     return export
  36.  
  37. def iniCDinifo(cdrom, cdcfile):
  38.     '''
  39.    光盘信息.ini格式化函数
  40.    @note: 直接利用os.walk()函数的输出信息由ConfigParser.RawConfigParser
  41.           进行重组处理或.ini格式文件输出并记录
  42.    @param cdrom: 光盘访问路径
  43.    @param cdcfile; 输出的光盘信息记录文件(包含路径, 绝对,相对都可以)
  44.    @return: 无, 直接输出成组织好的类.ini的*.cdc文件
  45.    '''
  46.     walker = {}
  47.     for root, dirs, files in os.walk(cdrom):
  48.         walker[root] = (dirs, files)
  49.     cfg = rcp()
  50.     cfg.add_section("Info")
  51.     cfg.add_section("Comment")
  52.     cfg.set("Info", "ImagePath", cdrom)
  53.     cfg.set("Info", "Volume", cdcfile)
  54.  
  55.     dirs = walker.keys()
  56.     i = 0
  57.     for d in dirs:
  58.         i += 1
  59.         cfg.set("Comment", str(i), d)
  60.  
  61.     for p in walker:
  62.         cfg.add_section(p)
  63.         for f in walker[p][1]:
  64.             cfg.set(p, f, os.stat("%s/%s" % (p, f)).st_size)
  65.     f = open(cdcfile, 'w')
  66.     cfg.write(f)
  67.     f.close()
  68.  
  69. def _smartcode(stream):
  70.     '''
  71.    智能能匹配编码
  72.    @param stream: 需要匹配的字符串
  73.    @return: 相应编码后的字符串
  74.    '''
  75.     ustring = stream
  76.     codedetect = chardet.detect(ustring)["encoding"]
  77.     #print codedetect
  78.     try:
  79.         #print ustring
  80.         ustring = unicode(ustring, codedetect)
  81.         #print ustring
  82.         return "%s %s " % ("", ustring.encode('gb2312'))
  83.         #return "%s %s " % ("", ustring.encode('utf8'))
  84.     except:
  85.         return u"bad unicode encode try!"
  86.  
  87. if __name__ =='__main__':
  88.     cdrom = "F:/Whu"
  89.     cdcfile = "F:/Whu/axiaoqiang.cdc"
  90.     cdWalker(cdrom, cdcfile)
  91.     #iniCDinifo(cdrom, cdcfile)
  92.  
  93.  
  94.    
  95. #//python/1186

Reply to "List of folders and files"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website