python学习,day5:内置模块(复制和压缩)

复制用的shutil模块

import shutil
f1 = open('本节笔记.txt',encoding='utf-8')

f2 = open("笔记2.txt",'w',encoding='utf-8')
shutil.copyfileobj(f1,f2)    #复制内容,需提前打开文件名

shutil.copyfile('笔记2.txt',"笔记3.txt")   #不需要打开,只需要输入文件名就能复制(内置了open程序)
shutil.copystat('笔记2.txt',"笔记3.txt")   #只copy权限
shutil.copy()    #文件和权限都copy

shutil.copytree('test4','newtest4')   #整个目录复制
shutil.rmtree('newtest4')    #删除目录
shutil.move()   #移动文件
shutil.make_archive("shutil_archive_test",'zip',r'C:\Users\lenovo\PycharmProjects\bjbsucc\day5')  #压缩

  压缩用的 zipfile模块

import  zipfile    #压缩的包

z= zipfile.ZipFile('day5.zip','w')  #打开压缩

z.write('t.py')                  #开始写入压缩包
print("----------")              #可以做别的
z.write('笔记2.txt')             #继续写入压缩包
z.close()                        #关闭压缩

 

posted @ 2019-08-15 17:10  bbgoal  阅读(152)  评论(0编辑  收藏  举报