导航:首页 > 编程语言 > pythongunzip

pythongunzip

发布时间:2022-04-25 23:50:47

python如何读取通过gzip压缩的字符串

import osimport gzip # 那是因为你调用了read方法,而这个方法会把文件一股脑儿读取出来的# 为了便于你迭代,你可以在这里使用一个生成器def read_gz_file(path): if os.path.exists(path): with gzip.open(path, 'rt') as pf: for line in pf: yield line else: print('the path [{}] is not exist!'.format(path)) con = read_gz_file('abc.gz')if getattr(con, '__iter__', None): for line in con: print(line, end = '')

Ⅱ Python gzip 压缩后,为什么反而更大了

因为数据量太小。而且不应该用sys.getsizeof来判断,应该用len(open(FILE_NAME, 'rb').read())来判断。sys.getsizeof是测量object的大小,不是纯数据。

Ⅲ 如何通过Python压缩解压缩zip文件

解压缩

importzipfile
importos
defun_zip(file_name):
"""unzipzipfile"""
zip_file=zipfile.ZipFile(file_name)
ifos.path.isdir(file_name+"_files"):
pass
else:
os.mkdir(file_name+"_files")
fornamesinzip_file.namelist():
zip_file.extract(names,file_name+"_files/")
zip_file.close()

打包

zipfile.ZipFile('xxx.zip','a/w/x').write('xxx.txt')

'w'以截断并写入新文件'a'以附加到现有文件,或'x'以专门创建和写入新文件。

Ⅳ python怎样压缩和解压缩ZIP文件(转)

榭梢越�姓庋�牟僮鳌2还� Python 中的 zipfile 模块不能处理多卷的情况,不过这种情况并不多见,因此在通常情况下已经足够使用了。下面我只是对一些基本的 zipfile 操作进行了记录,足以应付大部分的情况了。zipfile 模块可以让你打开或写入一个 zip 文件。比如:import zipfilez = zipfile.ZipFile('zipfilename', mode='r') 这样就打开了一个 zip 文件,如果mode为'w'或'a'则表示要写入一个 zip 文件。如果是写入,则还可以跟上第三个参数: compression=zipfile.ZIP_DEFLATED 或 compression=zipfile.ZIP_STORED ZIP_DEFLATED是压缩标志,如果使用它需要编译了zlib模块。而后一个只是用zip进行打包,并不压缩。在打开了zip文件之后就可以根据需要是读出zip文件的内容还是将内容保存到 zip 文件中。读出zip中的内容很简单,zipfile 对象提供了一个read(name)的方法。name为 zip文件中的一个文件入口,执行完成之后,将返回读出的内容,你把它保存到想到的文件中即可。写入zip文件有两种方式,一种是直接写入一个已经存在的文件,另一种是写入一个字符串。对 于第一种使用 zipfile 对象的 write(filename, arcname, compress_type),后两个参数是可以忽略的。第一个参数是文件名,第二个参数是表示在 zip 文件中的名字,如果没有给出,表示使用与filename一样的名字。compress_type是压缩标志,它可以覆盖创建 zipfile 时的参数。第二种是使用 zipfile 对象的 writestr(zinfo_or_arcname, bytes),第一个参数是zipinfo 对象或写到压缩文件中的压缩名,第二个参数是字符串。使用这个方法可以动态的组织文件的内容。类源码为:[python] view plain# coding:cp936 # Zfile.py # xxteach.com import zipfile import os.path import os class ZFile(object): def __init__(self, filename, mode='r', basedir=''): self.filename = filename self.mode = mode if self.mode in ('w', 'a'): self.zfile = zipfile.ZipFile(filename, self.mode, compression=zipfile.ZIP_DEFLATED) else: self.zfile = zipfile.ZipFile(filename, self.mode) self.basedir = basedir if not self.basedir: self.basedir = os.path.dirname(filename) def addfile(self, path, arcname=None): path = path.replace('//', '/') if not arcname: if path.startswith(self.basedir): arcname = path[len(self.basedir):] else: arcname = '' self.zfile.write(path, arcname) def addfiles(self, paths): for path in paths: if isinstance(path, tuple): self.addfile(*path) else: self.addfile(path) def close(self): self.zfile.close() def extract_to(self, path): for p in self.zfile.namelist(): self.extract(p, path) def extract(self, filename, path): if not filename.endswith('/'): f = os.path.join(path, filename) dir = os.path.dirname(f) if not os.path.exists(dir): os.makedirs(dir) file(f, 'wb').write(self.zfile.read(filename)) def create(zfile, files): z = ZFile(zfile, 'w') z.addfiles(files) z.close() def extract(zfile, path): z = ZFile(zfile) z.extract_to(path) z.close()

Ⅳ python压缩文件设置解压密码_zipfile.setpassword(bytes('pass',"utf-8"))为什么解压不用密码

setpassword()是在解压压缩包时的默认设置的解压密码

Ⅵ python如何判断一个文件是否为gzip文件

本文实例讲述了Python实现压缩与解压gzip大文件的方法。分享给大家供大家参考,具体如下:
#encoding=utf-8
#author: walker
#date: 2015-10-26
#summary: 测试gzip压缩/解压文件
import gzip
BufSize = 1024*8
def gZipFile(src, dst):
fin = open(src, 'rb')
fout = gzip.open(dst, 'wb')
in2out(fin, fout)
def gunZipFile(gzFile, dst):
fin = gzip.open(gzFile, 'rb')
fout = open(dst, 'wb')
in2out(fin, fout)
def in2out(fin, fout):
while True:
buf = fin.read(BufSize)
if len(buf) < 1:
break
fout.write(buf)
fin.close()
fout.close()
if __name__ == '__main__':
src = r'D:\tmp\src.txt'
dst = r'D:\tmp\src.txt.gz'
ori = r'D:\tmp\ori.txt'
gZipFile(src, dst)
print('gZipFile over!')
gunZipFile(dst, ori)
print('gunZipFile over!')

也可以简单地封装成一个类:
class GZipTool:
def __init__(self, bufSize):
self.bufSize = bufSize
self.fin = None
self.fout = None
def compress(self, src, dst):
self.fin = open(src, 'rb')
self.fout = gzip.open(dst, 'wb')
self.__in2out()
def decompress(self, gzFile, dst):
self.fin = gzip.open(gzFile, 'rb')
self.fout = open(dst, 'wb')
self.__in2out()
def __in2out(self,):
while True:
buf = self.fin.read(self.bufSize)
if len(buf) < 1:
break
self.fout.write(buf)
self.fin.close()
self.fout.close()

Ⅶ python压缩存储

美国 国务卿 克林顿 。 <> 美国(国务卿(希拉里 (竞选 (总统))(就职)(。))))

这行压缩后的 克林顿 不要了?

你只给可能数据,目前能做到这样子了(pyton3.3脚本)

Ⅷ python压缩成tar

Python压缩文件为tar、gzip的方源码。需要应用到os、tarfile、gzip、string、shutil这几个Python类库中的方法。不同于Python Gzip压缩与解压模块,今天我们要用自己的方法实现压...

Ⅸ python怎样压缩和解压缩ZIP文件

Python压缩ZIP文件:

importzipfile
f=zipfile.ZipFile(target,'w',zipfile.ZIP_DEFLATED)
f.write(filename,file_url)
f.close()

其中target:是压缩后要保存的路径,可以是: 'C:/temp/'
ZIP_DEFLATED:表示压缩,还有一个参数:ZIP_STORE:表示只打包,不压缩。

这个Linux中的gz跟tar格式有点类似.

write方法如果只有一个参数filename的话,表示把你filename所带的路径全部压缩到zip文件中。如果带两个参数,表示把filename路径中的那个file压缩一下并且存放到file_url中,中间没有增加任何的文件夹
如果要压缩很多的文件,循环的write就ok了, 最后close掉。
Python解压ZIP文件:

f=zipfile.ZipFile("zipfilePath",'r')
forfileinf.namelist():
f.extract(file,"temp/")

zipfilePath是压缩文件的路径
循环访问该压缩文件中的文件,并且一个一个file的解压到对应的"temp"文件夹中

阅读全文

与pythongunzip相关的资料

热点内容
解压包如何转音频 浏览:447
机明自动编程软件源码 浏览:325
php端口号设置 浏览:540
phperegreplace 浏览:319
androidgridview翻页 浏览:537
ssh协议编程 浏览:634
如何开我的世界电脑服务器地址 浏览:861
玄关pdf 浏览:609
程序员学习论坛 浏览:940
程序员的毒鸡汤怎么做 浏览:548
安卓怎么降级软件到手机 浏览:281
云与服务器入门书籍推荐产品 浏览:636
delphi编程助手 浏览:762
电脑遇到服务器问题怎么办 浏览:515
加工中心编程结束方法 浏览:296
了解什么是web服务器 浏览:140
面向对象的编程的基本特征 浏览:718
php定时执行任务linux 浏览:787
php数组中删除元素 浏览:725
萤石云服务器视频 浏览:270