Python中有一个gzip模块来处理gzip文件 读取gzip文件:
1234 |
import gzip f = gzip. open ( "tt.gz" , "rb" ) # 读取一行 f.readline()
|
GZIP:压缩一个文件
import gzip
1,f_in = open('iplist','rb')
2,f_out = gzip.open('iplist.gz','wb')
3,f_out.writelines(f_in)
4,f_out.close()
5,f_in.close()
6,f = gzip.open('iplist.gz','rb')
7,f.readline()
'121.8.102.126n'
8,f.readlines()
['182.128.179.162n', '121.9.15.146n', '125.118.221.146n', '61.139.124.66n', '183.145.208.217n', '14.153.145.49n', '116.25.192.98n', '183.7.109.62n', '218.17.162.217n', '58.213.141.183n', '61.164.166.122n', '222.95.180.41n', '182.149.234.40n', '60.177.22.212n', '180.118.29.64n', '115.197.116.39n', '222.212.49.130n', '14.216.123.197n', '14.20.31.77n', '183.22.105.212n', '219.134.89.66n', '220.167.53.179n', '183.45.111.38n', '125.120.32.98n', '180.109.64.209n']
bzip2:
写gzip文件
12345 |
import gzip f = gzip. open ( "tt.gz" , "wb" ) # 写入一行 f.write( "aaa" ) f.close()
|
写bzip2文件
12345 |
import bz2 f = bz2.BZ2File( "2.bz2" , "wb" ) # 写入一行 f.write( "xxx" ) f.close()
|
zip文件
zip的话,其实最主要的就是打包和解包操作。 解包zip文件:
12345 |
import zipfile x = zipfile.ZipFile( "1.zip" ) # 解包所有文件 x.extractall() x.close()
|
打包所有文件:
12345 |
x = zipfile.ZipFile( "2.zip" , "w" ) # 写入两个文件 x.write( "1.bz2" ) x.write( "2.gz" ) x.close()
|
操作tarfile
在我们平时的使用过程中,经常会使用tar来对整个目录进行gzip或者bzip2打包和解包。Python中有tarfile来操作这些。 举个例子,来解压一个.tar.gz文件:
import tarfile
123 |
t = tarfile. open ( "1.tgz" , "r:gz" ) t.extractall() t.close()
|