Python实现压缩文件夹与解压缩zip文件的方法


Posted in Python onSeptember 01, 2018

本文实例讲述了Python实现压缩文件夹与解压缩zip文件的方法。分享给大家供大家参考,具体如下:

直接上代码

#coding=utf-8
#甄码农python代码
#使用zipfile做目录压缩,解压缩功能
import os,os.path
import zipfile
def zip_dir(dirname,zipfilename):
  filelist = []
  if os.path.isfile(dirname):
    filelist.append(dirname)
  else :
    for root, dirs, files in os.walk(dirname):
      for name in files:
        filelist.append(os.path.join(root, name))
  zf = zipfile.ZipFile(zipfilename, "w", zipfile.zlib.DEFLATED)
  for tar in filelist:
    arcname = tar[len(dirname):]
    #print arcname
    zf.write(tar,arcname)
  zf.close()
def unzip_file(zipfilename, unziptodir):
  if not os.path.exists(unziptodir): os.mkdir(unziptodir, 0777)
  zfobj = zipfile.ZipFile(zipfilename)
  for name in zfobj.namelist():
    name = name.replace('\\','/')
    if name.endswith('/'):
      os.mkdir(os.path.join(unziptodir, name))
    else:
      ext_filename = os.path.join(unziptodir, name)
      ext_dir= os.path.dirname(ext_filename)
      if not os.path.exists(ext_dir) : os.mkdir(ext_dir,0777)
      outfile = open(ext_filename, 'wb')
      outfile.write(zfobj.read(name))
      outfile.close()
if __name__ == '__main__':
  zip_dir(r'E:/python/learning',r'E:/python/learning/zip.zip')
  unzip_file(r'E:/python/learning/zip.zip',r'E:/python/learning2')

运行后在E:/python/learning目录下生成zip.zip压缩文件,同时在E:/python目录下解压缩zip.zip文件到learning2目录。

希望本文所述对大家Python程序设计有所帮助。

Python 相关文章推荐
使用Python获取CPU、内存和硬盘等windowns系统信息的2个例子
Apr 15 Python
举例讲解Python设计模式编程中对抽象工厂模式的运用
Mar 02 Python
如何将python中的List转化成dictionary
Aug 15 Python
python清除字符串中间空格的实例讲解
May 11 Python
spark: RDD与DataFrame之间的相互转换方法
Jun 07 Python
python使用pygame框架实现推箱子游戏
Nov 20 Python
python mac下安装虚拟环境的图文教程
Apr 12 Python
python跳出双层for循环的解决方法
Jun 24 Python
django使用django-apscheduler 实现定时任务的例子
Jul 20 Python
Python 寻找局部最高点的实现
Dec 05 Python
python获取栅格点和面值的实现
Mar 10 Python
Python求区间正整数内所有素数之和的方法实例
Oct 13 Python
Python pymongo模块常用操作分析
Sep 01 #Python
Python实现提取XML内容并保存到Excel中的方法
Sep 01 #Python
python使用webdriver爬取微信公众号
Aug 31 #Python
python爬取微信公众号文章
Aug 31 #Python
Python单向链表和双向链表原理与用法实例详解
Aug 31 #Python
Python使用Flask-SQLAlchemy连接数据库操作示例
Aug 31 #Python
浅谈Python traceback的优雅处理
Aug 31 #Python
You might like
基于mysql的论坛(5)
2006/10/09 PHP
通过5个php实例细致说明传值与传引用的区别
2012/08/08 PHP
php使用curl检测网页是否被百度收录的示例分享
2014/01/31 PHP
CI框架中通过hook的方式实现简单的权限控制
2015/01/07 PHP
php curl获取https页面内容,不直接输出返回结果的设置方法
2019/01/15 PHP
dtree 网页树状菜单及传递对象集合到js内,动态生成节点
2012/04/14 Javascript
5分钟理解JavaScript中this用法分享
2013/11/09 Javascript
基于jquery实现等比缩放图片
2014/12/03 Javascript
使用FlexiGrid实现Extjs表格效果方法分享
2014/12/16 Javascript
AngularJs定制样式插入到ueditor中的问题小结
2016/08/01 Javascript
EasyUI为Numberbox添加blur事件的方法
2017/03/05 Javascript
Bootstrap Table使用整理(一)
2017/06/09 Javascript
浅谈事件冒泡、事件委托、jQuery元素节点操作、滚轮事件与函数节流
2017/07/22 jQuery
使用jquery DataTable和ajax向页面显示数据列表的方法
2018/08/09 jQuery
20个必会的JavaScript面试题(小结)
2019/07/02 Javascript
vue v-for直接循环数字实例
2019/11/07 Javascript
[52:32]完美世界DOTA2联赛PWL S2 Magma vs LBZS 第三场 11.18
2020/11/18 DOTA
Python实现判断字符串中包含某个字符的判断函数示例
2018/01/08 Python
python使用turtle库与random库绘制雪花
2018/06/22 Python
用Python配平化学方程式的方法
2019/07/20 Python
Python获取统计自己的qq群成员信息的方法
2019/11/15 Python
python GUI库图形界面开发之PyQt5表单布局控件QFormLayout详细使用方法与实例
2020/03/06 Python
python 获取剪切板内容的两种方法
2020/11/28 Python
Omio葡萄牙:全欧洲低价大巴、火车和航班搜索和比价
2019/02/09 全球购物
成人毕业生自我鉴定
2013/10/18 职场文书
医院保洁服务方案
2014/06/11 职场文书
关于读书的演讲稿400字
2014/08/27 职场文书
活动总结格式
2014/08/30 职场文书
车辆转让协议书
2014/09/24 职场文书
银行授权委托书格式
2014/10/10 职场文书
小学生安全教育广播稿
2014/10/20 职场文书
临床医学生职业规划书范文
2014/10/25 职场文书
党的群众路线教育实践活动个人对照检查材料(四风)
2014/11/05 职场文书
大学生活委员竞选稿
2015/11/21 职场文书
Python包argparse模块常用方法
2021/06/04 Python
实战Python爬虫爬取酷我音乐
2022/04/11 Python