#/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)
阅读(1291) | 评论(0) | 转发(0) |