解压文件夹python

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

import zipfile
import shutil
import os
print os.getcwd()
basedir = os.path.dirname(__file__)

print os.path.dirname(os.path.dirname(__file__))


def unzip_file(zipfilename,unziptodir):
    if not os.path.exists(unziptodir):
        os.mkdir(unziptodir,0777)
    zfobj = zipfile.ZipFile(zipfilename)
    for name in zfobj.namelist():
        name = name.replace('\\','/')
        if name.endswith('/'):
            print name
            os.mkdir(os.path.join(unziptodir,name))
        else:
            ext_filename = os.path.join(unziptodir,name)
            ext_dir = os.path.dirname(ext_filename)
            if not os.path.exists(ext_dir):
                os.mkdir(ext_dir,0777)
            outfile = open(ext_filename,'wb')
            outfile.write(zfobj.read(name))
            outfile.close()
def deledir():
    current_path = os.path.split(os.path.realpath(__file__))[0]
    current_filelist = os.listdir(current_path)
    for f in current_filelist:
        if os.path.isdir(f):
            real_folder_path = os.path.join(current_path,f)
            try:
                for root,dirs,files in os.walk(real_folder_path):
                    for name in files:
                        del_file = os.path.join(root,name)
                        os.remove(del_file)
                    shutil.rmtree(real_folder_path)
if __name__ == '__main__':
    
    unzip_file(r'd:\temp\Android.zip',r'E:\temp\liuzhi')

    

 

posted @ 2017-06-01 20:33  liuzhipenglove  阅读(551)  评论(0编辑  收藏  举报