遍历文件夹下压缩文件并解压至指定文件夹

# -*- coding:utf-8 -*-

import Cons as cs
import os
import zipfile
#########  Extract all files in Dir
def extract_to(src_dir,des_dir):
    #os.chdir(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):
            print dir_tmp
            f = zipfile.ZipFile(dir_tmp, mode="r")
            print f.namelist()[0]
            filename = f.namelist()[0]
            if filename.startswith("HGUPERIODIC"):
                f.extract( member=filename, path=des_dir)
        else:
            extract_to(dir_tmp,des_dir)
    return 0


##Test
if __name__ == "__main__":
    extract_to(src_dir=cs.src_dir,des_dir=cs.des_dir)

你可能感兴趣的:(python)