Chinaunix首页 | 论坛 | 博客
  • 博客访问: 269149
  • 博文数量: 103
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 705
  • 用 户 组: 普通用户
  • 注册时间: 2013-05-02 16:15
文章分类

全部博文(103)

文章存档

2014年(8)

2013年(95)

我的朋友

分类: Python/Ruby

2013-11-26 12:41:38

#/usr/bin/env
#coding:utf8
import tarfile,os


def create_tar(destdir,filename):
        tar =tarfile.open(filename,"w:gz")
        for root,dir,files in os.walk(destdir):
                for file in files:
                        fullpath=os.path.join(root,file)
                        tar.add(fullpath)
        tar.close()


def unpack_tar(dirs,tarname):
        if os.path.exists(dirs):
                os.chdir(dirs)
        else:
                os.mkdir(dirs)
                os.chdir(dirs)
        tar=tarfile.open(dirs+'/'+tarname)
        tar.extractall()
        tar.close()
if __name__ == "__main__":
        dirs="/data/python/xml/001"
        tarname="hans.tar.gz"
        #需要备份的目标目录
        destdir="/data/python/xml"
        #备份的文件名
        filename="hans.tar.gz"


        create_tar(destdir,filename)
        unpack_tar(dirs,tarname)

阅读(1251) | 评论(0) | 转发(0) |
0

上一篇:python-打印制定行文本

下一篇:python-pingall

给主人留下些什么吧!~~