rar_command = 'winrar a -r %s %s' % (target,source)
改为
rar_command = 'tar -zcvf %s %s' % (target,source)
Ⅱ python 在不解压的情况下,怎么得知压缩包里面文件的大小呢
可以使用 tarfile 模块在不解压的情况下查看压缩包里文件的大小,代码如下:
#coding=utf8
importtarfile
importos
tar=tarfile.open('G:/test/abc.tar.gz','r:gz')
fortiintar:
ifti.isreg():
print(ti.name,ti.size)
Ⅲ 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如何按行获取gz包中的数据
importos
importgzip
#那是因为你调用了read方法,而这个方法会把文件一股脑儿读取出来的
#为了便于你迭代,你可以在这里使用一个生成器
defread_gz_file(path):
ifos.path.exists(path):
withgzip.open(path,'rt')aspf:
forlineinpf:
yieldline
else:
print('thepath[{}]isnotexist!'.format(path))
con=read_gz_file('abc.gz')
ifgetattr(con,'__iter__',None):
forlineincon:
print(line,end='')
Ⅳ python 如何批量解压多个gz压缩包
长按,后来继续点击解压就可以全部解压。
Ⅵ 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压缩成tar
Python压缩文件为tar、gzip的方源码。需要应用到os、tarfile、gzip、string、shutil这几个Python类库中的方法。不同于Python Gzip压缩与解压模块,今天我们要用自己的方法实现压...
Ⅷ python 解压缩tar.gz文件 判断文件是否正常
Python自带的tarfile模块可以方便读取tar归档文件,牛b的是可以处理使用gzip和bz2压缩归档文件tar.gz和tar.bz2。
对于gz格式gz文件一般不加密常见的是这种.tar.gz格式的 对于tar格式 在python标准库我没找到关于检测加密的方法
Ⅸ python tar.gz怎么安装
tar.gz文件是在linux系统下的文件格式
.tar是把文件打成一个包,并不压缩;
.gz是用gzip把打成包的.tar文件压缩,所以成了一个.tar.gz的文件。
安装的话,先解包,tar -zxvf python.tar.gz,这样会生成一个以文件名命名的文件夹。
有的文件直接这样解压即可,例如tomcat、eclipse
有的文件在解压之后的文件中 有运行的脚本文件,如:start.sh 运行即可