1. 程式人生 > >[python實用小工具]python解壓zip檔案

[python實用小工具]python解壓zip檔案

使用python解壓zip檔案,帶密碼或者不帶密碼都可行,適合批量解壓時使用

# @Date    : 2018-08-31 14:45:58
# @Author  : Jimy_Fengqi ([email protected])
# @Link    : https://blog.csdn.net/qiqiyingse
# @Version : V1.0

import zipfile

filename=['0.zip','1.zip','2.zip','3.zip','5.zip']
def unzip(filename):
	'''
	parameter:
		filename  需要解壓的檔案
	'''
	file_zip=zipfile.ZipFile(filename,'r')
	if len(file_zip.namelist()) <= 1:
		zipchildname=file_zip.namelist()[0]
		print('the zipfile [%s] only have one file, the file name is  %s' % (filename,zipchildname))
		try:
			file_zip.extract(zipchildname,'')
		except:
			try:
				password='123'
				print('the zipfile [%s] need password to  extract, current password is %s' % (filename,password))
				file_zip.extractall(pwd=bytes(password,"utf-8"))
			except:
				print('error , extract zipfile with password failed,  maybe need change password')
		file_zip.close()
		return zipchildname
	else:
		print(' the zipfile [%s]  has %d files ' % (filename,len(file_zip.namelist())))
		try:
			file_zip.extractall()
		except:
			try:
				password='123'
				print('the zipfile [%s] need password to  extract, current password is %s' % (filename,password))
				file_zip.extractall(pwd=bytes(password,"utf-8")) 
			except:
				print('error , extract zipfile with password failed,  maybe need change password')
		return file_zip.namelist()