Python下使用tarfile模块来实现文件归档压缩与解压
2014-12-15 22:37
751 查看
Python下使用tarfile模块来实现文件归档压缩与解压
部分转自:http://www.diybl.com/course/3_program/python/20110510/555345.html
Tarfile.open(cls, name=None, mode='r', fileobj=None, bufsize=10240, **kwargs) method of __builtin__.type instance
Open a tar archive for reading, writing or appending. Return
an appropriate TarFile class.
mode:
'r' or 'r:*' open for reading with transparent compression
'r:' open for reading exclusively uncompressed
'r:gz' open for reading with gzip compression
'r:bz2' open for reading with bzip2 compression
'a' or 'a:' open for appending, creating the file if necessary
'w' or 'w:' open for writing without compression
'w:gz' open for writing with gzip compression
'w:bz2' open for writing with bzip2 compression
'r|*' open a stream of tar blocks with transparent compression
'r|' open an uncompressed stream of tar blocks for reading
'r|gz' open a gzip compressed stream of tar blocks
'r|bz2' open a bzip2 compressed stream of tar blocks
'w|' open an uncompressed stream for writing
'w|gz' open a gzip compressed stream for writing
'w|bz2' open a bzip2 compressed stream for writing
1.压缩,创建tar.gz包
#!/usr/bin/env python
import os
import tarfile
#创建压缩包名
tar = tarfile.open("/tmp/tartest.tar.gz","w:gz")
#创建压缩包
for root,dir,files in os.walk("/tmp/tartest"):
for file in files:
fullpath = os.path.join(root,file)
tar.add(fullpath)
tar.close()
2.解压tar.gz包
#!/usr/bin/env python
import tarfile
tar = tarfile.open(“/tmp/tartest.tar.gz”)
names = tar.getnames()
for name in names:
tar.extract(name,path=”/tmp”)
tar.close()
部分转自:http://www.diybl.com/course/3_program/python/20110510/555345.html
Tarfile.open(cls, name=None, mode='r', fileobj=None, bufsize=10240, **kwargs) method of __builtin__.type instance
Open a tar archive for reading, writing or appending. Return
an appropriate TarFile class.
mode:
'r' or 'r:*' open for reading with transparent compression
'r:' open for reading exclusively uncompressed
'r:gz' open for reading with gzip compression
'r:bz2' open for reading with bzip2 compression
'a' or 'a:' open for appending, creating the file if necessary
'w' or 'w:' open for writing without compression
'w:gz' open for writing with gzip compression
'w:bz2' open for writing with bzip2 compression
'r|*' open a stream of tar blocks with transparent compression
'r|' open an uncompressed stream of tar blocks for reading
'r|gz' open a gzip compressed stream of tar blocks
'r|bz2' open a bzip2 compressed stream of tar blocks
'w|' open an uncompressed stream for writing
'w|gz' open a gzip compressed stream for writing
'w|bz2' open a bzip2 compressed stream for writing
1.压缩,创建tar.gz包
#!/usr/bin/env python
import os
import tarfile
#创建压缩包名
tar = tarfile.open("/tmp/tartest.tar.gz","w:gz")
#创建压缩包
for root,dir,files in os.walk("/tmp/tartest"):
for file in files:
fullpath = os.path.join(root,file)
tar.add(fullpath)
tar.close()
2.解压tar.gz包
#!/usr/bin/env python
import tarfile
tar = tarfile.open(“/tmp/tartest.tar.gz”)
names = tar.getnames()
for name in names:
tar.extract(name,path=”/tmp”)
tar.close()
相关文章推荐
- Python下使用tarfile模块来实现文件归档压缩与解压
- Python下使用tarfile模块来实现文件归档压缩与解压
- Python中使用tarfile压缩、解压tar归档文件示例
- Python中使用tarfile压缩、解压tar归档文件示例
- Python中使用tarfile压缩、解压tar归档文件
- python 8-3-1 使用tarfile可以实现文件的压缩打包
- python 8-3-1 使用tarfile可以实现文件的压缩打包
- Python自动化运维笔记(五):使用filecmp模块实现文件以及文件目录差异对比
- C#使用SharpZipLib压缩和解压文件3/3调用实现压缩和解压
- python的tarfile模块实例 python把文件夹压缩成tar格式文件的例子
- python的tarfile模块实例 python把文件夹压缩成tar格式文件的例子
- Python(15)python使用tkinter实现一个简单的压缩/解压程序
- python使用zlib实现压缩与解压字符串
- 使用Python3解压gz、tar、tgz、zip、rar五种格式的压缩文件例子
- 利用python中的gzip模块压缩和解压数据流和文件
- 使用SharpZipLib实现文件压缩、解压
- python用模块zlib压缩与解压字符串和文件的方法
- shell脚本中if与case使用,查找文件locate与find的使用,压缩,解压及归档工具
- python_使用tarfile压缩及解压的方法
- Python实现多级目录压缩与解压文件的方法