[python实用小工具]python解压zip文件

使用python解压zip文件,带密码或者不带密码都可行,适合批量解压时使用

# @Date    : 2018-08-31 14:45:58
# @Author  : Jimy_Fengqi (jmps515@163.com)
# @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()

 

posted @ 2018-08-31 14:47  枫奇丶宛南  阅读(46)  评论(0编辑  收藏  举报