#!/usr/bin/env python3
# coding: utf-8
import os, tarfile
# 把名为folder文件压缩成tar.gz格式
def make_targz(output_filename, source_dir):
"""
一次性打包目录为tar.gz
:param output_filename: 压缩文件名
:param source_dir: 需要打包的目录
:return: bool
"""
try:
with tarfile.open(output_filename, "w:gz") as tar:
tar.add(source_dir, arcname=os.path.basename(source_dir))
return True
except Exception as e:
print(e)
return False
make_targz('aa.tar.gz', 'folder')
# 解压成folder文件
def untar(fname, dirs):
"""
解压tar.gz文件
:param fname: 压缩文件名
:param dirs: 解压后的存放路径
:return: bool
"""
try:
t = tarfile.open(fname)
t.extractall(path = dirs)
return True
except Exception as e:
print(e)
return False
untar('aa.tar.gz', './')
12-28
06-28
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交