模块讲解----shutil模块(copy、压缩、解压)
作用与功能
主要用于文件的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()
模块讲解----shutil模块(copy、压缩、解压)的更多相关文章
- 模块 shutil_zipfile_tarfile压缩解压
shutil_zipfile_tarfile压缩解压 shutil 模块 高级的 文件.文件夹.压缩包 处理模块 shutil.copyfileobj(fsrc, fdst[, length]) #将 ...
- python之模块之shutil模块
shutil -- --High-level file operations 高级的文件操作模块. os模块提供了对目录或者文件的新建/删除/查看文件属性,还提供了对文件以及目录的路径操作.比如说: ...
- day5模块学习--shutil模块
shutil模块 高级的 文件.文件夹.压缩包 处理模块 os模块提供了对目录或者文件的新建/删除/查看文件属性,还提供了对文件以及目录的路径操作.比如说:绝对路径,父目录…… 但是,os文件的操作 ...
- python常用模块之shutil模块
python常用模块之shutil模块 shutil模块,高级的文件.文件夹.压缩包处理模块 1.shutil.copyfile(src,des[,length]):将文件内容拷贝到另一个文件 In ...
- python入门之sys模块、shutil模块
sys模块 import sys sys.version 返回python的版本 sys.argv 返回一个以脚本名,和传入的参数作为元素的列表 sys.path 返回一个以当前代码文件路径,pyth ...
- python对文件的压缩解压
python自带的zipfile的模块支持对文件的压缩和解压操作 zipfilp.ZipFile 表示创建一个zip对象 zipfile.ZipFile(file[, mode[, compressi ...
- os模块和shutil模块
# coding=utf-8 import os path="D:\\test" ######### 目录结构如下 # test # / \ \ \ # test01 test02 ...
- Java压缩/解压.zip、.tar.gz、.tar.bz2(支持中文)
本文介绍Java压缩/解压.zip..tar.gz..tar.bz2的方式. 对于zip文件:使用java.util.zip.ZipEntry 和 java.util.zip.ZipFile,通过设置 ...
- [转]python中对文件、文件夹的操作——os模块和shutil模块常用说明
转至:http://l90z11.blog.163.com/blog/static/187389042201312153318389/ python中对文件.文件夹的操作需要涉及到os模块和shuti ...
随机推荐
- e2fsprogs 移植
e2fsprogs是用维护ext2,ext3和ext4文件系统的工具程序集.检测和修复文件系统,需要用到其中的fsck, ext2fs等工具, 由于开发板上没有,重新制作文件系统又比较麻烦.所以就需要 ...
- js 版本号
在web项目开发过程中,我们经常会引用css.js文件,更新文件后常出现缓存问题(明明更改了代码,在浏览器上访问的时候却没有发生变化),这种情况我们通常采用以下两种解决方案: 1.手动清除浏览器缓存 ...
- hdu 2821(dfs)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2821 思路:一开始的时候没注意到,必须从map[i][j]==0的位置开始,然后就是dfs了,回溯的时 ...
- 敏捷开发Scrum学习
官方:http://baike.baidu.com/link?url=VGFzdJpuHX3g90kIX6l1QABWMmBNyf30sTGuEcJ6OJVMq0Cot1G9Imbu1gls-xpI6 ...
- poj_1860 SPFA
题目大意 有N种货币,M个兑换点,每个兑换点只能相互兑换两种货币.设兑换点A可以兑换货币C1和C2,给出rate(C1--C2)表示1单位的C1货币可以兑换出的C2货币数目,rate(C2--C1)表 ...
- github 's usage
author:headsen chen date: 2018-05-30 10:50:56 notice:This article is created by headsen chen him ...
- 【BZOJ3831】[Poi2014]Little Bird 单调队列
[BZOJ3831][Poi2014]Little Bird Description In the Byteotian Line Forest there are trees in a row. ...
- PHP疑难杂症
下面这种写法是否允许? echo '\n' // \n echo "\n" // 输出换行 直接访问对象不存在的属性,会怎样? $o = new stdClass(); echo ...
- CH5E01 乌龟棋【线性DP】
5E01 乌龟棋 0x5E「动态规划」练习 描述 小明过生日的时候,爸爸送给他一副乌龟棋当作礼物.乌龟棋的棋盘是一行N 个格子,每个格子上一个分数(非负整数).棋盘第1 格是唯一的起点,第N 格是终点 ...
- Requset和Response中的乱码问题
在我们的日常开发中,乱码问题,还是比较经常遇到的,有时候是浏览器端提交的数据到后台乱码了,有时候是后台响应的数据到前台浏览器端展现出现乱码了.下面我们将通过几个简单的例子来说明乱码的由来和解决方式. ...