闵大荒搬砖中
全部博文(23)
分类: Python/Ruby
2014-06-26 16:27:28
写gzip文件
- import gzip
- f = gzip.open("1.gz", "rb")
- # 读取一行
- f.readline()
- import gzip
- f = gzip.open("2.gz", "wb")
- # 写入一行
- f.write("xxx")
- f.close()
写bzip2文件
- import bz2
- f = bz2.BZ2File("1.bz2")
- # 读取一行
- f.readline()
- import bz2
- f = bz2.BZ2File("2.bz2", "wb")
- # 写入一行
- f.write("xxx")
- f.close()
打包所有文件:
- import zipfile
- x = zipfile.ZipFile("1.zip")
- # 解包所有文件
- x.extractall()
- x.close()
- x = zipfile.ZipFile("2.zip", "w")
- # 写入两个文件
- x.write("1.bz2")
- x.write("2.gz")
- x.close()
tar.file.open的mode有很多种:
- t = tarfile.open("1.tgz", "r:gz")
- t.extractall()
- t.close()
mode | action |
---|---|
'r' or 'r:*' | Open for reading with transparent compression (recommended). |
'r:' | Open for reading exclusively without compression. |
'r:gz' | Open for reading with gzip compression. |
'r:bz2' | Open for reading with bzip2 compression. |
'a' or 'a:' | Open for appending with no compression. The file is created if it does not exist. |
'w' or 'w:' | Open for uncompressed writing. |
'w:gz' | Open for gzip compressed writing. |
'w:bz2' | Open for bzip2 compressed writing. |