python gzip压缩文件

下面的代码实现用gzip格式压缩文件,需要引用gzip包。

#!
import string
import gzip
from optparse import OptionParser#
# version 1.01
#created by T.Newell 06/11/2007
# after searching the web ASPN and not finding a simple example of using gzip to zip a file i created this little gem.
#def readCommandLine():parser = OptionParser()#read the options inparser.add_option("-f","--Full_file_location",dest="File_to_be_run",default=r"c:\tn.txt",help="This is the fully qualified path name to the file location")parser.add_option("-m","--Mode",dest="modeTn",default="r",help="The mode of zip unzip")parser.add_option("-c","--Compression",dest="compress",default=9,help="The level of compression")options, args = parser.parse_args()#print optionsreturn optionsdef zipit(filename, mode,compress):#Saves/Zipps a compressed file to disk#r_file = open(filename, 'r')# this is the zipping bitw_file = gzip.GzipFile(filename + '.gz', mode, compress)w_file.write(r_file.read())w_file.flush()w_file.close()r_file.close()def un_zipit(filename,mode):#Unzips a compressed file from disk##this is the unzipping bit  r_file = gzip.GzipFile(filename, mode)write_file = string.rstrip(filename, '.gz')w_file = open(write_file, 'w')w_file.write(r_file.read())w_file.close()r_file.close()if __name__ == "__main__":#first thing to do is read the options inoptions = readCommandLine()if options.modeTn == "r":#unzippit modeif options.File_to_be_run[-3:] != '.gz':# check to see if it has the extension .gzprint "This " + options.File_to_be_run + " is not a .gz file"else:#This should now unzipitun_zipit(options.File_to_be_run,options.modeTn)elif options.modeTn== "wb":#this should zipitzipit(options.File_to_be_run,options.modeTn,options.compress)else:# basically the wrong option was passedprint "ABORT something went wrong"sys.exit()

python gzip压缩文件相关推荐

  1. Python将Pandas中Dataframe数据保存为gzip/zip文件:gzip压缩文件、zip压缩文件

    Python将Pandas中Dataframe数据保存为gzip/zip文件:gzip压缩文件.zip压缩文件 目录 Python将Pandas中Dataframe数据保存为gzip/zip文件:gz ...

  2. python读写压缩文件使用gzip和bz2

    python读写压缩文件使用gzip和bz2 #读取压缩文件 # gzip compression import gzip with gzip.open('somefile.gz', 'rt') as ...

  3. 用Python创建gzip压缩文件

    用Python创建gzip压缩文件 准备把所有文章中的图片链接全部更改到二级域名上.因为数据量比较大,为了方便修改,先将数据库内容导出为数据库脚本文件(后缀名为.sql),直接批量替换后再通过phpM ...

  4. Python的压缩文件处理 zipfile tarfile

    本文从以下两个方面, 阐述Python的压缩文件处理方式: 一. zipfile 二. tarfile 一. zipfile 虽然叫zipfile,但是除了zip之外,rar,war,jar这些压缩( ...

  5. python读压缩文件内容_Python读写压缩文件的方法

    问题 你想读写一个gzip或bz2格式的压缩文件. 解决方案 gzip 和bz2模块可以很容易的处理这些文件. 两个模块都为 open()函数提供了另外的实现来解决这个问题. 比如,为了以文本形式读取 ...

  6. python gzip压缩_Python gzip –压缩解压缩

    python gzip压缩 Python gzip module provides a very simple way to compress and decompress files and wor ...

  7. pandas使用read_csv读取数据使用skiprows参数跳过指定的数据行但保留表头、pandas使用to_csv函数将dataframe保存为gzip压缩文件

    pandas使用read_csv读取数据使用skiprows参数跳过指定的数据行但保留表头.pandas使用to_csv函数将dataframe保存为gzip压缩文件 目录

  8. 检查压缩包是否损坏_修复损坏的gzip压缩文件之原理篇

    接修复损坏的gzip压缩文件之原理篇,再次引用GZIP结构图: 已知修复一个损坏的gzip文件的关键环节在于找到下一个正常压缩包的起始点.根据结构图中的信息可知,每个压缩包的开始结构中有是否到达尾部标 ...

  9. gzip压缩文件损坏修复原理和数据恢复方法

    接修复损坏的gzip压缩文件之原理篇,再次引用GZIP结构图: 已知修复一个损坏的gzip文件的关键环节在于找到下一个正常压缩包的起始点.根据结构图中的信息可知,每个压缩包的开始结构中有是否到达尾部标 ...

最新文章

  1. 5年前的Dubbo,2年前的Spring Cloud,都输给了这个架构!
  2. linux进去网卡,Linux上使用socket进行网卡抓包
  3. 【高清】网络安全思维导图
  4. 微软企业库连接access,web.config相对路径配置
  5. 智慧交通day02-车流量检测实现01:总览
  6. Flink编程入门(二)
  7. matlab 导入元胞,MATLAB导入xls文件以及cell的使用方法
  8. JAVA分布式篇2——Zookeeper
  9. 怎么设置电脑壁纸_电脑怎么设置双屏或多屏显示?
  10. python(12)给文件读写上锁
  11. python 通达信公式函数,python通达信公式函数,python调用通达信数据
  12. django使用ldap进行用户登录认证
  13. CSS实现盒子居中对齐的七种方法
  14. 魔改010Editor Template 识别伪加密
  15. vc开发记牌器的两种思路
  16. 基于Pytorch构建一个可训练的BNN
  17. 【Oracle】并行等待之PX Deq: Signal ACK
  18. 计算一阶导数的四阶中心差分格式
  19. 我想死你们了,大家好!
  20. 玩转Google开源C++单元测试框架

热门文章

  1. 面试题10:二进制中1的个数
  2. serialable 和 parcelable详解
  3. Android 对Layout_weight属性完全解析以及使用ListView来实现表格
  4. listview中有checkbox 滑动后状态改变
  5. react中使用charles实现本地数据mock
  6. Django例子-出版社
  7. 温故知新ASP.NET 2.0(C#)(1) - MasterPage(母版页)
  8. postgresql 用户安全配置
  9. [转]IDE之zend studio: zend studio8使用感受
  10. 谷歌修复已遭利用的 0day