(转)Python3之shutil模块
原文:https://www.cnblogs.com/wang-yc/p/5625046.html
一. 简介
shutil 是高级的文件,文件夹,压缩包处理模块。
二. 使用
shutil.copyfileobj(fsrc, fdst[, length])
将文件内容拷贝到另一个文件中
|
1
2
3
|
import shutil shutil.copyfileobj(open('old.xml','r'), open('new.xml', 'w')) |
shutil.copyfile(src, dst)
拷贝文件
|
1
|
shutil.copyfile('f1.log', 'f2.log') |
shutil.copymode(src, dst)
仅拷贝权限。内容、组、用户均不变
|
1
|
shutil.copymode('f1.log', 'f2.log') |
shutil.copystat(src, dst)
仅拷贝状态的信息,包括:mode bits, atime, mtime, flags
|
1
|
shutil.copystat('f1.log', 'f2.log') |
shutil.copy(src, dst)
拷贝文件和权限
|
1
|
shutil.copy('f1.log', 'f2.log') |
shutil.copy2(src, dst)
拷贝文件和状态信息
|
1
|
shutil.copy2('f1.log', 'f2.log') |
shutil.ignore_patterns(*patterns)
shutil.copytree(src, dst, symlinks=False, ignore=None)
递归的去拷贝文件夹
|
1
2
3
|
shutil.copytree('folder1', 'folder2', ignore=shutil.ignore_patterns('*.pyc', 'tmp*'))shutil.copytree('f1', 'f2', symlinks=True, ignore=shutil.ignore_patterns('*.pyc', 'tmp*')) |
shutil.rmtree(path[, ignore_errors[, onerror]])
递归的去删除文件
|
1
|
shutil.rmtree('folder1') |
shutil.move(src, dst)
递归的去移动文件,它类似mv命令,其实就是重命名。
|
1
|
shutil.move('folder1', 'folder3') |
shutil.make_archive(base_name, format,...)
创建压缩包并返回文件路径,例如:zip、tar
创建压缩包并返回文件路径,例如:zip、tar
- base_name: 压缩包的文件名,也可以是压缩包的路径。只是文件名时,则保存至当前目录,否则保存至指定路径,
如:www =>保存至当前路径
如:/Users/wupeiqi/www =>保存至/Users/wupeiqi/ - format: 压缩包种类,“zip”, “tar”, “bztar”,“gztar”
- root_dir: 要压缩的文件夹路径(默认当前目录)
- owner: 用户,默认当前用户
- group: 组,默认当前组
- logger: 用于记录日志,通常是logging.Logger对象
|
1
2
3
4
5
6
7
8
|
#将 /Users/wupeiqi/Downloads/test 下的文件打包放置当前程序目录import shutilret = shutil.make_archive("wwwwwwwwww", 'gztar', root_dir='/Users/wupeiqi/Downloads/test') #将 /Users/wupeiqi/Downloads/test 下的文件打包放置 /Users/wupeiqi/目录import shutilret = shutil.make_archive("/Users/wupeiqi/wwwwwwwwww", 'gztar', root_dir='/Users/wupeiqi/Downloads/test') |
shutil 对压缩包的处理是通过调用ZipFile 和 TarFile两个模块来进行的。
|
1
2
3
4
5
6
7
8
9
10
11
12
|
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()z.close() |
|
1
2
3
4
5
6
7
8
9
10
11
12
|
import tarfile# 压缩tar = tarfile.open('your.tar','w')tar.add('/Users/wupeiqi/PycharmProjects/bbs2.log', arcname='bbs2.log')tar.add('/Users/wupeiqi/PycharmProjects/cmdb.log', arcname='cmdb.log')tar.close()# 解压tar = tarfile.open('your.tar','r')tar.extractall() # 可设置解压地址tar.close() |
(转)Python3之shutil模块的更多相关文章
- Python3之shutil模块
一. 简介 shutil 是高级的文件,文件夹,压缩包处理模块. 二. 使用 shutil.copyfileobj(fsrc, fdst[, length])将文件内容拷贝到另一个文件中 import ...
- Python3 shutil模块
平时我们总会用到复制文件的命令,Python中自带了相应模块,那就是shutil模块,下面是shutil模块的分析及用法. 1.copyfileobj(fsrc, fdst, length=16*10 ...
- python(6)-shutil模块
高级的 文件.文件夹.压缩包 处理模块 shutil.copyfileobj(fsrc, fdst[, length]) 将文件内容拷贝到另一个文件中: #源码 def copyfileobj(fsr ...
- s14 第5天 时间模块 随机模块 String模块 shutil模块(文件操作) 文件压缩(zipfile和tarfile)shelve模块 XML模块 ConfigParser配置文件操作模块 hashlib散列模块 Subprocess模块(调用shell) logging模块 正则表达式模块 r字符串和转译
时间模块 time datatime time.clock(2.7) time.process_time(3.3) 测量处理器运算时间,不包括sleep时间 time.altzone 返回与UTC时间 ...
- Python第二十天 shutil 模块 zipfile tarfile 模块
Python第二十天 shutil 模块 zipfile tarfile 模块 os文件的操作还应该包含移动 复制 打包 压缩 解压等操作,这些os模块都没有提供 shutil 模块shut ...
- python中的shutil模块
目录 python中的shutil模块 目录和文件操作 归档操作 python中的shutil模块 shutil模块对文件和文件集合提供了许多高级操作,特别是提供了支持文件复制和删除的函数. 目录和文 ...
- Python进阶5---StringIO和BytesIO、路径操作、OS模块、shutil模块
StringIO StringIO操作 BytesIO BytesIO操作 file-like对象 路径操作 路径操作模块 3.4版本之前:os.path模块 3.4版本开始 建议使用pathlib模 ...
- python之模块之shutil模块
shutil -- --High-level file operations 高级的文件操作模块. os模块提供了对目录或者文件的新建/删除/查看文件属性,还提供了对文件以及目录的路径操作.比如说: ...
- shutil模块和os模块对比
一.shutil -- 是一种高层次的文件操作工具类似于高级API,而且主要强大之处在于其对文件的复制与删除操作更是比较支持好. 1.shutil.copy(src,dst)复制一个文件到另一个目录下 ...
随机推荐
- MapServer和GeoServer对比
https://blog.csdn.net/theonegis/article/details/45823099
- 批量插入,批量修改的sql
sql 1 批量插入 <insert id="batchInsert" useGeneratedKeys="true" parameterType=&q ...
- 【python-字典】判断python字典中key是否存在的
一般有两种通用做法: 第一种方法:使用自带函数实现: 在python的字典的属性方法里面有一个has_key()方法: #生成一个字典 d = {'name':Tom, 'age':10, 'Tel' ...
- vue.js入门学习
可以用淘宝npm镜像 然后安装 然后初始化项目: Watch就是一个监听 v-if是如果为false就根本不在页面存在这个元素 v-show是通过display:none来控制这个元素的显示和隐藏 r ...
- html 语法
p: 源代码中包含多行,但是浏览器会忽略多行 <br />产生折行效果 h1:居中用样式实现 :水平线,有的很像border-top/bottom,或许用 实现更好 pre: 预格式化的文 ...
- Flashcache基本使用及注意事项
Flashcache基本使用及注意事项 发表回复 环境:Centos6.5 x64 minal 安装方法 Contents [hide] 1 安装方法 2 自动加载模块 3 创建Flashcache ...
- 8.css内容移出与精灵图
定位的盒子居中显示 ★:margin:0 auto; 只能让标准流的盒子居中对齐. ★定位的盒子居中:先左右走父元素盒子的一半50%,在向左走子盒子的一半(margin-left:负值.) 标签包含 ...
- Android-Java-面向对象与面向过程的简单理解
支持面向过程的语言有:C Basic 等语言: 支持面向对象的语言有:C++ Java C# 等语言: 面向过程:操作的是行为/功能: 面向对象:操作的是对象,而对象里面有功能行为,所以可以指定 ...
- 【xcode中添加pch全局引用文件】
前沿:xcode6中去掉了pch,为了一些琐碎的头文件引用,加快了 编译速度! xcode6之前的版本建项目就自动添加了是这样的: xcode6后的版本要自己手动的添加步骤如下: 1) 2) 3) ...
- 机器学习实战-ch3-决策树
决策树是一种新算法: 优点:计算复杂度不高,输出结果易于理解,对中间值的缺失不敏感,可以处理不相关特征数据.缺点:可能会产生过度匹配问题. 决策树算法可用于数据类型:数值型和标称型. 决策树的核心在于 ...