Python3之shutil模块
来源:互联网 发布:淘宝男t恤店推荐 知乎 编辑:程序博客网 时间:2024/06/05 11:47
一. 简介
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
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'
)
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()
阅读全文
0 0
- Python3之shutil模块
- Python3之shutil模块
- Python3之shutil模块
- Python3之shutil模块
- Python3.5——内置模块详解之os模块、sys模块、shutil模块
- python之shutil模块
- python之shutil模块
- # Python3 shutil(高级文件操作 模块)
- Python3 shutil(高级文件操作 模块)
- python之模块之shutil模块
- shutil模块
- shutil模块
- shutil模块
- shutil模块
- python模块系列之 - os , os.path,shutil
- Python shutil模块
- python shutil模块学习
- [python] shutil模块
- 计数排序
- JS基础知识
- mt6735 [CTS Fail]BuildFingerprint
- Lua函数
- 如何获取系统时间,年月日时分秒?
- Python3之shutil模块
- UVA 11174 Stand in a Line——组合数+取模
- PHP5.6 配置文件php.ini详解(译版)
- 解决XCode编译异常clang: error: linker command failed with exit code 1
- 跟着示例学Oozie
- HP elitebook 840 G3 蓝牙无法使用问题 + 触摸板无法关闭
- DES算法简介及android中的实现
- Spring注解详解
- 使用eclipse提交项目到GitHub