python解压TAR文件至指定文件夹的实例

如下所示:

######### Extract all files from src_dir to des_dir
def extract_tar_files(src_dir,des_dir):
  files = os.listdir(src_dir)
  for file in files:
    dir_tmp = os.path.join(src_dir, file)
    print dir_tmp
    if not os.path.isdir(dir_tmp): ##是文件,非文件夹
      #解压特定文件
      if dir_tmp.endswith("gz") and (dir_tmp.find(cs.Port_week_perfer_name_start) != -1):
        #f = zipfile.ZipFile(dir_tmp, mode="r")
        f = tarfile.open(dir_tmp)
        names = f.getnames()
        for name in names:
          f.extract(name, path=des_dir)
    else:
      extract_tar_files(dir_tmp,des_dir)
  return 0

相关推荐