def unzip_file(fname, dirs):
import zipfile
fz = zipfile.ZipFile(fname, 'r')
# for file in fz.namelist():
# print(file)
# fz.extract(file, dirs)
fz.extractall(dirs)
def ungz_file(fname, dirs):
t = tarfile.open(fname, "r:*")
names = t.getnames()
print(names)
t.extractall(path=dirs)
t.close()
def tar_gz(fname):
"""
创建gz文件
:param fname:
:return:
"""
import tarfile
t = tarfile.open(fname + ".tar.gz", "w:gz")
for root, dir_list, files in os.walk(fname):
if not files:
arc_name = root.replace(fname, "")
t.add(root, arcname=arc_name)
else:
for file in files:
arc_name = file.replace(fname, "")
full_path = os.path.join(root, file)
t.add(full_path, arcname=arc_name)
t.close()
原文地址:https://www.cnblogs.com/lajiao/p/10670336.html
时间: 2024-11-11 17:47:44