#_*_ coding:utf-8 _*_ImportZipFileImportShutilImportOSPrintos.getcwd () basedir= Os.path.dirname (__file__)PrintOs.path.dirname (Os.path.dirname (__file__))defUnzip_file (zipfilename,unziptodir):if notos.path.exists (Unziptodir): Os.mkdir (Unziptodir,0777) Zfobj=ZipFile. ZipFile (Zipfilename) forNameinchzfobj.namelist (): Name= Name.replace ('\\','/') ifName.endswith ('/'): Printname Os.mkdir (Os.path.join (unziptodir,name))Else: Ext_filename=Os.path.join (unziptodir,name) Ext_dir=os.path.dirname (ext_filename)if notos.path.exists (Ext_dir): Os.mkdir (Ext_dir,0777) outfile= Open (Ext_filename,'WB') Outfile.write (zfobj.read (name)) Outfile.close ()defDeledir (): Current_path= Os.path.split (Os.path.realpath (__file__)) [0] Current_filelist=Os.listdir (Current_path) forFinchcurrent_filelist:ifOs.path.isdir (f): Real_folder_path=Os.path.join (current_path,f)Try: forRoot,dirs,filesinchOs.walk (real_folder_path): forNameinchFiles: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')
Unzip folder Python