python 解压zip文件脚本

574 阅读1分钟

Windows Server 2008 R2 系统自带的Powershell脚本是2.0版本,无法调用本地一些解压缩软件解压zip文件,但是可以调用python,具体的解压命令如下所示

# -*- coding: utf-8 -*-.
import os
import shutil
import zipfile
from os.path import join, getsize

def unzip_file(zip_src, dst_dir):
    r = zipfile.is_zipfile(zip_src)
    if r:     
        fz = zipfile.ZipFile(zip_src, 'r')
        for file in fz.namelist():
            fz.extract(file, dst_dir)       
    else:
        print('This is not zip')

def remove_files():
    if os.path.exists("d:\\UNZIP\\index.html"):
        os.remove("d:\\UNZIP\\index.html")
        shutil.rmtree('D:\\UNZIP\\static',ignore_errors=True)
    else:
        print("error")
remove_files()
unzip_file("d:\\UNZIP\\dist.zip","d:\\UNZIP")