import shutil
#用于简化文件操作的模块
# f1 = open(r"D:\上海python全栈4期\day20\7.shutil模块.py","rb")
# f2 = open(r"D:\上海python全栈4期\day20\7.shutil模块2.py","wb")
# shutil.copyfileobj(f1,f2)
# 压缩文件
# shutil.make_archive("my压缩","zip",r"D:\上海python全栈4期\day20")
# shutil.make_archive("my压缩","tar",r"D:\上海python全栈4期\day20\pack")
# 用shutil直接解压
shutil.unpack_archive(r"D:\上海python全栈4期\day20\you压缩.zip")
# 解压 可以使用对应的模块来完成
# import zipfile
# z = zipfile.ZipFile(r"D:\上海python全栈4期\day20\my压缩.zip","r") #打开文件
# z.extractall(path=r"D:\上海python全栈4期\day20\解压") #提取文件
# z.close() # 关闭文件
# import tarfile
# t = tarfile.open(r"D:\上海python全栈4期\day20\my压缩.tar","r")
# t.extractall(r"D:\上海python全栈4期\day20\解压")
# t.close()
# import tarfile
# t = tarfile.open(r"D:\上海python全栈4期\day20\you压缩.tar","w")
# t.add("1.昨日回顾")
# t.add("2.今日内容")
# t.close()
# import zipfile
# z = zipfile.ZipFile(r"D:\上海python全栈4期\day20\you压缩.zip","w")
# z.write("1.昨日回顾")
# z.write("2.今日内容")
# z.close()
压缩文件的另一种方法:
# 压缩文件
# shutil.make_archive("my压缩","zip",r"D:\上海python全栈4期\day20")
# shutil.make_archive("my压缩","tar",r"D:\上海python全栈4期\day20\pack")
# 解压 需要使用对应的模块来完成
# import zipfile
# z = zipfile.ZipFile(r"D:\上海python全栈4期\day20\my压缩.zip","r")
# z.extractall(path=r"D:\上海python全栈4期\day20\解压")
# z.close()
import tarfile
t = tarfile.open(r"D:\上海python全栈4期\day20\my压缩.tar","r")
t.extractall(r"D:\上海python全栈4期\day20\解压")
t.close()