The zip file is one of the packaging formats that we often use, and it is efficient to extract and compress the python zip. Python Unzip zip document:
Copy Code code as follows:
#/usr/bin/python
#coding =utf-8
Import Os,sys,time
Import ZipFile
filename = ' Callofdutyblackopszombies_1349649132343_my.zip ' #要解压的文件
Filedir = ' data/' #解压后放入的目录
r = zipfile.is_zipfile (filename)
If R:
StartTime = Time.time ()
FZ = ZipFile. ZipFile (filename, ' R ')
For file in Fz.namelist ():
Print (file) #打印zip归档中目录
Fz.extract (File,filedir)
Endtime = Time.time ()
Times = Endtime-starttime
Else
Print (' This file was not zip file ')
Print (' Times ' + str)
The Python compressed folder is zip
Copy Code code as follows:
#/usr/bin/python
#coding =utf-8
Import OS
Import ZipFile
Import Sys
Try
Import zlib
Compression = ZipFile. zip_deflated
Except
Compression = ZipFile. Zip_stored
Path = ' data/' #要进行压缩的文档目录
Start = Path.rfind (os.sep) + 1
filename = ' Callofdutyblackopszombies_1349649132343_my.zip ' #压缩后的文件名
z = zipfile. ZipFile (Filename,mode = "W", compression = compression)
Try
For dirpath,dirs,files in Os.walk (path):
For file in Files:
if file = = FileName or file = = "zip.py":
Continue
Print (file)
Z_path = Os.path.join (dirpath,file)
Z.write (Z_path,z_path[start:])
Z.close ()
Except
If z:
Z.close ()