11 python shutil 模块
shutil 模块 高级的 文件、文件夹、压缩包 处理模块
1、将文件内容拷贝到另一个文件中
import shutil
f1 = open('os_模块.py','r',encoding='utf-8')
f2 = open('for_test.py','w',encoding='utf-8')
shutil.copyfileobj(f1,f2)
2、拷贝文件 将A文件的内容直接copy到B
shutil.copyfile('f1.log', 'f2.log') #目标文件无需存在
shutil.copymode(src, dst)
import shutil
shutil.copyfile('os_模块.py','for_test.py')
shutil.copyfile(文件1,文件2):不用打开文件,直接用文件名进行覆盖co
仅拷贝权限。内容、组、用户均不变
shutil.copymode('f1.log', 'f2.log') #目标文件必须存在 shutil.copystat(src, dst)
仅拷贝状态的信息,包括:mode bits, atime, mtime, flags
shutil.copystat('f1.log', 'f2.log') #目标文件必须存在 shutil.copy(src, dst)
拷贝文件和权限
import shutil
shutil.copy('f1.log', 'f2.log') shutil.copy2(src, dst)
拷贝文件和状态信息
import shutil
shutil.copy2('f1.log', 'f2.log')
3、递归的去拷贝文件夹,一层一层的copy
shutil.ignore_patterns(*patterns)
shutil.copytree(src, dst, symlinks=False, ignore=None)
递归的去拷贝文件夹,一层一层的copy
import shutil
shutil.copytree('folder1', 'folder2', ignore=shutil.ignore_patterns('*.pyc', 'tmp*'))
#目标目录不能存在,注意对folder2目录父级目录要有可写权限,ignore的意思是排除
4、递归的去删除和移动文件
shutil.rmtree(path[, ignore_errors[, onerror]])
递归的去删除文件
import shutil
shutil.rmtree('folder1') shutil.move(src, dst)
递归的去移动文件,它类似mv命令,其实就是重命名。
import shutil
shutil.move('folder1', 'folder3')
5、创建压缩包并返回文件路径,例如:zip、tar
base_name: 压缩包的文件名,也可以是压缩包的路径。只是文件名时,则保存至当前目录,否则保存至指定路径,
如 data_bak =>保存至当前路径
如:/tmp/data_bak =>保存至/tmp/ format: 压缩包种类,“zip”, “tar”, “bztar”,“gztar”
root_dir: 要压缩的文件夹路径(默认当前目录)
owner: 用户,默认当前用户
group: 组,默认当前组
logger: 用于记录日志,通常是logging.Logger对象
#将 /data 下的文件打包放置当前程序目录
import shutil
ret = shutil.make_archive("data_bak", 'gztar', root_dir='/data') #将 /data下的文件打包放置 /tmp/目录
import shutil
ret = shutil.make_archive("/tmp/data_bak", 'gztar', root_dir='/data')
6、压缩包的处理是调用 ZipFile 和 TarFile 两个模块
import zipfile # 压缩
z = zipfile.ZipFile('laxi.zip', 'w')
z.write('a.log')
z.write('data.data')
z.close() # 解压
z = zipfile.ZipFile('laxi.zip', 'r')
z.extractall(path='.')
z.close()
import tarfile # 压缩
>>> t=tarfile.open('/tmp/egon.tar','w')
>>> t.add('/test1/a.py',arcname='a.bak')
>>> t.add('/test1/b.py',arcname='b.bak')
>>> t.close() # 解压
>>> t=tarfile.open('/tmp/egon.tar','r')
>>> t.extractall('/egon')
>>> t.close()

11 python shutil 模块的更多相关文章
- python shutil模块简单介绍
python shutil模块简单介绍 简介 shutil模块提供了大量的文件的高级操作.特别针对文件拷贝和删除,主要功能为目录和文件操作以及压缩操作. shutil 模块方法: copy(src, ...
- Python shutil模块
shutil模块下 copy(复制).rm(删除).move(移动) 常用方法举例. copyfileobj(fsrc, fdst[, length])copyfile(src, dst, *, fo ...
- python shutil模块&random模块
shutil模块 import shutil shutil.copyfileobj(open("os_.py", "r"), open("os_2.p ...
- Python shutil模块(目录和文件操作)
import shutil #导入shutil模块 copyfileobj方法 将类文件对象fsrc的内容复制到类文件对象fdst shutil.copyfileobj(fsrc, fdst[, le ...
- (11)shutil模块(文件处理模块)
shutil模块的格式 shutil.copyfileobj(文件1,文件2) #将文件1的数据覆盖copy给文件2 import shutil f1 = open("1.txt&quo ...
- python shutil 模块 的剪切文件函数 shutil.movemove(src, dst),换用 os.rename(sourceFile, targetFile)
Google 一搜python 剪切文件,出来shutil 这模块,网上很多人也跟疯说shutil.move(src, dst)就是用来剪切文件的,结果一试,剪切毛线,文件都复制到另一个文件夹了,源文 ...
- python—shutil模块
该模块拥有许多文件或文件的删除.移动.复制.重命名等功能. 1.copy():复制文件 格式:shutil.copy(来源文件,目标地址) 返回值:返回复制之后的路径 2.copy2():复制文件和状 ...
- [转]Python shutil 模块
转自: https://www.cnblogs.com/wuzhiblog/p/6535527.html https://www.cnblogs.com/caibao666/p/6433864.htm ...
- Python shutil 模块学习笔记
学于https://automatetheboringstuff.com shutil 名字来源于 shell utilities,有学习或了解过Linux的人应该都对 shell 不陌生,可以借此来 ...
随机推荐
- c实现windows socket
服务端代码: /* * testSocketService.c * * Created on: 2012-8-16 * Author: 皓月繁星 */ #include <WINSOCK2.H& ...
- python安装途中遇到的问题和解决方法
一.setuptools安装错误:RuntimeError: Compression requires the (missing) zlib module 1. 描述 搞了个腾讯云的服务器,闲在手上没 ...
- c++从string类型转换为bool类型
利用输入字符串流istringstream bool b; string s="true"; istringstream(s)>>boolalpha>>b; ...
- weex-toolkit 使用
weex-toolkit 的github地址:https://github.com/weexteam/weex-toolkit weex-toolkit: 初始化的项目是针对开发单个 Weex 页面而 ...
- windows python3 安装gittle
1 从github clone gittle项目, git clone https://github.com/FriendCode/gittle.git 2 进行到下载的项目,执行安装 python ...
- Microsoft Dynamics CRM4.0 和 Microsoft Dynamics CRM 2011 JScript 方法对比
CRM 2011 如果需要再IE里面调试,可以按F12在前面加上contentIFrame,比如 contentIFrame.document.getElementById("字段" ...
- java 静态导入
- Android画图之抗锯齿 paint 和 Canvas 两种方式
在画图的时候,图片如果旋转或缩放之后,总是会出现那些华丽的锯齿.其实Android自带了解决方式. 方法一:给Paint加上抗锯齿标志.然后将Paint对象作为参数传给canvas的绘制方法. ...
- maven学习(2)-在Eclipse 中使用Maven
第一节:m2eclipse 插件安装 打开Eclipse,点击菜单Help - > Install New Software 点击Add 按钮Name:m2e location: http:// ...
- java web程序 String的valueOf方法总集
在代码中用到类型转换的时候,是一个字符,然后当用户在网页中输入的是字符串, 字符转换成字符串的方法是: String.valueOf(char c);就好了 这样在写验证码的时候,网页端的就是字符串形 ...