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 運行即可