模块讲解----shutil模块(copy、压缩、解压)

时间:2024-04-28 19:49:40

作用与功能

主要用于文件的copy,压缩,解压

导入shuitl模块:

import shutil

copy方法

 1、shutil.copyfileobj()  打开file1,并copy写入file2:
with open("笔记1",'r',encoding='utf-8') as f1,open('笔记2','w',encoding='utf-8') as f2:
   shutil.copyfileobj(f1,f2) #输入文件名就能直接拷贝(调用copyfileobj方法)
shutil.copyfile("笔记1","笔记3") #拷贝权限,内容,组,用户均不变:(win看不出来,linux下可以尝试)
shutil.copymode("笔记1","笔记3") #拷贝状态的信息(只拷贝权限,不创建文件),包括:mode bits,atime,mtime,flags
shutil.copystat("笔记1","笔记3") #拷贝文件和权限:
shutil.copy("笔记1","笔记3") #拷贝文件和状态信息:(文件和权限)
shutil.copy2("笔记1","笔记3") #递归的去copy文件:(copy目录)
shutil.copytree(r"D:\a",r"D:\a1")
例如:用python脚本实现代码发布指定线上服务器,例如svn和git在发布的时候,有些文件是不需要进行拷贝的,因袭就需要进行过滤
方法如下:
shutil.copytree('f1', 'f2', symlinks=True, ignore=shutil.ignore_patterns('*.pyc', 'tmp*')) #递归的删除目录:(有链接*目录,删除报错)
shutil.rmtree(r"D:\a1") #移动文件:
shutil.move(r"D:\a",r"D:\a1")

压缩和解压缩方法

 1、全目压缩:
#创建压缩包,并返回文件路径:例如:zip tar
#创建压缩包并返回文件路径,例如:zip、tar
#格式:shutil.make_archive(base_name,format(zip),root_dir,owner,group,logger)
# base_name: 压缩包的文件名,也可以是压缩包的路径。只是文件名时,则保存至当前目录,否则保存至指定路径,
# 如:www                       =>保存至当前路径
# 如:/Users/wupeiqi/www =>保存至/Users/wupeiqi/
# format: 压缩包种类,“zip”, “tar”, “bztar”,“gztar”
# root_dir: 要压缩的文件夹路径(默认当前目录)
# owner: 用户,默认当前用户
# group: 组,默认当前组
# logger: 用于记录日志,通常是logging.Logger对象
#将 /Users/wupeiqi/Downloads/test 下的文件打包放置当前程序目录 import shutil
ret = shutil.make_archive("wwwwwwwwww", 'gztar', root_dir='/Users/wupeiqi/Downloads/test')

#将 /Users/wupeiqi/Downloads/test 下的文件打包放置 /Users/wupeiqi/目录
import shutil
ret = shutil.make_archive("/Users/wupeiqi/wwwwwwwwww", 'gztar', root_dir='/Users/wupeiqi/Downloads/test')

#将D:\软件\pychar\data\s13\Atm目录下的文件打包放置在D:\软件\pychar\data\s13\Atm_name_tar文件下
shutil.make_archive(r"D:\软件\pychar\data\s13\Atm_name_tar","tar","D:\软件\pychar\data\s13\Atm") ==========================================================================
三、解压方法和指定文件的压缩和解压: 2、Zip单个文件压缩与解压:(打包在压缩)
(压缩包也可以当做一个文件,想要加入压缩文件的话可以直接写进压缩包里) 2.1:#写入指定压缩文件(w)
z = zipfile.ZipFile(r'D:\软件\pychar\data\test\node.zip', 'w')
z.write('笔记1')
z.write('笔记3')
z.close() 2.2 #追加指定压缩(a)
z = zipfile.ZipFile(r'D:\软件\pychar\data\test\node.zip', 'a')
z.write('test.py')
z.write('md_sys_test.py')
z.close() 2.3:z.extractall()   解压所有文件:(所有文件)
os.chdir(r"D:\软件\pychar\data\test")
z = zipfile.ZipFile("node.zip",'r')
z.extractall()  
z.close() 2.4:z.extract('test.py')  解压指定文件:
只需要传输字符串格式的文件名即可
os.chdir(r"D:\软件\pychar\data\test")
z = zipfile.ZipFile("node.zip",'r')
for item in z.namelist():
   if item == 'test.py':
       z.extract('test.py')
z.close() =============================================================================================
3、tar单个文件压缩与解压:(tar只打包不压缩)
3.1、写入指定压缩文件(w)
import tarfile

tar = tarfile.open(r'D:\软件\pychar\data\test\your.tar','w')
tar.add(r'D:\软件\pychar\data\test\test.py', arcname='bbs2.log')
tar.add(r'D:\软件\pychar\data\test\md_sys_test.py', arcname='cmdb.log')
tar.close() 3.2、添加指定压缩文件(a)
tar = tarfile.open(r'D:\软件\pychar\data\test\your.tar','a')
tar.add(r'D:\软件\pychar\data\test\笔记1', arcname='node1.txt')
tar.add(r'D:\软件\pychar\data\test\笔记3', arcname='node3.txt')
tar.close() 3.3、解压所有文件
os.chdir(r"D:\软件\pychar\data\test")
tar = tarfile.open('your.tar','r')
tar.extractall()  # 可设置解压地址
tar.close() 3.4、解压指定文件
tar.getmembers():遍币所有压缩包内的文件对象(非文件字符串)
tar.getmember("node1.txt"):指定压缩包内的某个文件
os.chdir(r"D:\软件\pychar\data\test")
tar = tarfile.open('your.tar','r')
for item in tar.getmembers():
   job = tar.getmember("node1.txt")
   if item == job:
       tar.extract(job)
tar.close()