python gzip压缩文件,pythongzip,下面的代码实现用gzip
python gzip压缩文件,pythongzip,下面的代码实现用gzip
下面的代码实现用gzip格式压缩文件,需要引用gzip包。
#!import stringimport gzipfrom 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 in parser.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 options return optionsdef zipit(filename, mode,compress): #Saves/Zipps a compressed file to disk # r_file = open(filename, 'r') # this is the zipping bit w_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 in options = readCommandLine() if options.modeTn == "r": #unzippit mode if options.File_to_be_run[-3:] != '.gz': # check to see if it has the extension .gz print "This " + options.File_to_be_run + " is not a .gz file" else: #This should now unzipit un_zipit(options.File_to_be_run,options.modeTn) elif options.modeTn== "wb": #this should zipit zipit(options.File_to_be_run,options.modeTn,options.compress) else: # basically the wrong option was passed print "ABORT something went wrong" sys.exit()
相关内容
- python导出linux用户权限,pythonlinux,要换linux服务器,由
- python目录遍历代码,python目录,如下python实现目录
- Python 正则表达式(re模块)操作指南,pythonre,原文出处:
- python使用126发邮件代码,python126发邮件,写了个用smtpl
- python读写文件方法介绍,python读写介绍,open 打开文件使
- 动态修改python系统路径,动态python路径,默认情况下py
- Python RGB 和HSV颜色相互转换,pythonhsv,Python RGB 和
- python对MySqldb模块的简单封装代码,pythonmysqldb封装,#!/
- python使用pymongo连接MongoDB,pymongomongodb,本文是一个Pyth
- django-redis-cache:用Redis作django的缓存层,redisdjango,djan
评论关闭