aifengqi

使用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()

 

分类:

技术点:

相关文章:

  • 2021-11-23
  • 2021-09-20
  • 2021-11-09
  • 2021-11-21
  • 2021-11-09
  • 2021-11-21
  • 2021-10-16
  • 2021-11-21
猜你喜欢
  • 2021-12-01
  • 2021-11-21
  • 2021-12-01
  • 2021-11-30
  • 2021-11-21
  • 2021-12-09
  • 2021-08-10
相关资源
相似解决方案