Java 企業應用
          不要溫柔的走入那個良夜
          import zipfile, tarfile
             
           (1)讀取一個zip壓縮包的指定文件,如果這個文件是XML格式,使用DOM從中取數據
          def get_ifix_ids_by_zip_path(fix_path):
              ifix_ids 
          = []
              zf 
          = zipfile.ZipFile(fix_path)
              
          try:
                  repository 
          = zf.read("repository.xml")
                  repo 
          = xml.dom.minidom.parseString(repository)
                  repositoryDigest 
          = repo.getElementsByTagName("repositoryDigest")[0]
                  
          for x in repositoryDigest.childNodes:
                      
          try:
                          ifix_ids.append(x.getElementsByTagName(
          "fix")[0].getAttribute("id"))
                      
          except:
                          
          pass
              
          except:
                  e 
          = sys.exc_info()
                  logger.error(_(
          "%s is not a valid interim fix; ignore it when you are installing interim fixes."% fix_path)
                  logger.debug(e)
              
          return ifix_ids
          (2) 判斷一個文件是否是tar或zip
          def is_compressedfile(f):
              
          if os.path.isfile(f):
                  
          return zipfile.is_zipfile(f) or tarfile.is_tarfile(f)
              
          return False
          (3)檢查一個zip文件是否包含指定文件名的文件
           ifix_zip = zipfile.ZipFile(ifix, 'r')
                      
          return True if "repository.xml" in [x.filename for x in ifix_zip.infolist()] else False
          (4)解壓zip文件到指定的目錄
          with open(ifix["path"], "rb") as candidate:
                              zipfile.ZipFile(candidate).extractall(ifix_path)
          (5)解壓tar文件到指定的目錄
          tarfile.open(ifix["path"], 'r|gz').extractall(path=ifix_path)
          posted on 2013-07-24 09:47 cpegtop 閱讀(390) 評論(0)  編輯  收藏 所屬分類: AggressionPython

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
           
          主站蜘蛛池模板: 东方市| 龙口市| 邮箱| 田阳县| 太仆寺旗| 斗六市| 海宁市| 安西县| 永康市| 孟州市| 余干县| 贵南县| 达尔| 察哈| 武胜县| 盈江县| 合阳县| 广东省| 泰宁县| 靖宇县| 武清区| 哈巴河县| 双江| 屏山县| 吴江市| 弥勒县| 淅川县| 东乡族自治县| 车险| 富阳市| 昌宁县| 南汇区| 汽车| 驻马店市| 油尖旺区| 南安市| 石狮市| 汝南县| 泌阳县| 利津县| 桐柏县|