Chinaunix首页 | 论坛 | 博客
  • 博客访问: 382576
  • 博文数量: 58
  • 博客积分: 2941
  • 博客等级: 少校
  • 技术积分: 970
  • 用 户 组: 普通用户
  • 注册时间: 2005-12-21 11:37
文章分类

全部博文(58)

文章存档

2015年(1)

2014年(1)

2012年(1)

2011年(19)

2009年(1)

2008年(1)

2007年(11)

2006年(10)

2005年(13)

分类: Python/Ruby

2011-04-18 11:00:00

 

1.压缩,创建tar.gz

  1. #!/usr/bin/env python



  2. import os

  3. import tarfile



  4. #创建压缩包名

  5. tar = tarfile.open("/tmp/tartest.tar.gz","w:gz")



  6. #创建压缩包

  7. for root,dir,files in os.walk("/tmp/tartest"):

  8.         for file in files:

  9.                 fullpath = os.path.join(root,file)

  10.                 tar.add(fullpath)

  11. tar.close

 

2.解压tar.gz

 

  1. #!/usr/bin/env python



  2. import tarfile



  3. tar = tarfile.open(/tmp/tartest.tar.gz”)

  4. names = tar.getnames()

  5. for name in names:

  6.        tar.extract(name,path=/tmp”)

  7. tar.close()
阅读(8986) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~