python基础--shutil模块
shutil模块提供了大量的文件的高级操作。
特别针对文件拷贝和删除,主要功能为目录和文件操作以及压缩操作。对单个文件的操作也可参见os模块。
注意
即便是更高级别的文件复制函数(shutil.copy(),shutil.copy2())也不能复制所有文件的元数据。
这意味着在linux平台上,文件的所有者和组以及访问控制列表都将丢失。
在Mac OS中资源fork和其他元数据无法使用。这意味着资源将丢失,文件类型和创建者代码将不正确。
在Windows上,文件所有者,ACL和备用数据流不会被复制。
1)shutil.copyfileobj(src, dst[, length])
将文件内容拷贝到另一个文件中,可以部分内容.
复制文件内容(不包含元数据)从类文件对象src到类文件对dst。
可选参数length指定缓冲区的大小,负数表示一次性读入。
默认会把数据切分成小块拷贝,以免占用太多内存。
length默认值:length=16*1024
注意:拷贝是从src的当前文件开始
import shutil
r_file=open("test1.py",'r',encoding="utf-8")
w_file=open("test22.py",'w',encoding="utf-8")
shutil.copyfileobj(r_file,w_file,2)
2)shutil.copyfile(src, dst)
拷贝文件
shutil.copyfile(src, dst):复制文件内容(不包含元数据)从src到dst。 DST必须是完整的目标文件名;拷贝目录参见shutil.copy()。
如果src和dst是同一文件,就会引发错误shutil.Error。dst必须是可写的,否则将引发异常IOError。
如果dst已经存在,它会被替换。特殊文件,例如字符或块设备和管道不能使用此功能,因为copyfile会打开并阅读文件。 src和dst的是字符串形式的路径名。
例子如下:
shutil.copyfile("test1.py","test2-2.py")
3)shutil.copymode(src, dst)
仅拷贝权限。内容、组、用户均不变
root@store1 python]# cat 1.txt
1111111111122222222
[root@store1 python]# python
Python 3.6.0 (default, Feb 16 2017, 20:26:37)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import shutil
>>> shutil.copy("1.txt","2.txt")
'2.txt'
>>> exit [root@store1 python]# ls -l
total 8
-rw-r--r--. 1 root root 20 May 13 21:16 1.txt
-rw-r--r--. 1 root root 20 May 13 21:17 2.txt
4)shutil.copystat(src, dst)
拷贝状态的信息,包括:mode bits, atime, mtime, flags。dst文件必须存在。注意结果和copymode对比。copymode时间是不一样的。copystat时间同步
>>> shutil.copystat("1.txt","2.txt")
>>>
[root@store1 python]# ls -l
total 8
-rw-r--r--. 1 root root 20 May 13 21:16 1.txt
-rw-r--r--. 1 root root 20 May 13 21:16 2.txt
5)shutil.copy(src, dst)
拷贝文件和权限。调用copyfile(src, dst)和copymode(src, dst)
[root@store1 python]# python
Python 3.6.0 (default, Feb 16 2017, 20:26:37)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import shutil
>>> shutil.copy("1.txt","3.txt")
'3.txt'
>>>
[root@store1 python]# ls -l
total 12
-rw-r--r--. 1 root root 20 May 13 21:16 1.txt
-rw-r--r--. 1 root root 20 May 13 21:16 2.txt
-rw-r--r--. 1 root root 20 May 13 21:31 3.txt
6)shutil.copy2(src, dst)
拷贝文件和状态信息,调用copyfile(src, dst)和copystat(src, dst)
7)shutil.copytree(src, dst, symlinks=False, ignore=None)
递归的去拷贝文件
drwxr-xr-x. 2 root root 45 May 13 21:38 dir1
[root@store1 python]# python
Python 3.6.0 (default, Feb 16 2017, 20:26:37)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import shutil
>>> shutil.copytree("dir1","dir2")
'dir2'
>>>
[root@store1 python]# ls -R
.:
dir1 dir2 ./dir1:
1.txt 2.txt 3.txt ./dir2:
1.txt 2.txt 3.txt
[root@store1 python]#
8)shutil.rmtree(path[, ignore_errors[, onerror]])
递归的去删除文件
[root@store1 python]# tree
.
├── dir1
│ ├── 1.txt
│ ├── 2.txt
│ └── 3.txt
└── dir2
├── 1.txt
├── 2.txt
└── 3.txt 2 directories, 6 files
[root@store1 python]# python
Python 3.6.0 (default, Feb 16 2017, 20:26:37)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import shutil
>>> shutil.rmtree("dir2")
>>>
[root@store1 python]# tree
.
└── dir1
├── 1.txt
├── 2.txt
└── 3.txt 1 directory, 3 files
[root@store1 python]#
9)shutil.move(src, dst)
递归的去移动文件
[root@store1 python]# tree
.
├── dir1
│ ├── 1.txt
│ ├── 2.txt
│ └── 3.txt
└── dir2 2 directories, 3 files
[root@store1 python]# python
Python 3.6.0 (default, Feb 16 2017, 20:26:37)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import shutil
>>> shutil.move("dir1/1.txt","dir2")
'dir2/1.txt'
>>>
[root@store1 python]# tree
.
├── dir1
│ ├── 2.txt
│ └── 3.txt
└── dir2
└── 1.txt 2 directories, 3 files
[root@store1 python]#
10)shutil.make_archive(base_name, format,...)
创建压缩包并返回文件路径,例如:zip、tar
base_name: 压缩包的文件名,也可以是压缩包的路径。只是文件名时,则保存至当前目录,否则保存至指定路径,
如:www =>保存至当前路径
如:/Users/wupeiqi/www =>保存至/Users/wupeiqi/
format: 压缩包种类,“zip”, “tar”, “bztar”,“gztar”
root_dir: 要压缩的文件夹路径(默认当前目录)
owner: 用户,默认当前用户
group: 组,默认当前组
logger: 用于记录日志,通常是logging.Logger对象
[root@store1 python]# tree
.
├── dir1
│ ├── 2.txt
│ └── 3.txt
└── dir2
└── 1.txt 2 directories, 3 files
[root@store1 python]# python
Python 3.6.0 (default, Feb 16 2017, 20:26:37)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import shutil
>>> shutil.make_archive("dir1","zip")
'dir1.zip'
>>> shutil.make_archive("dir1","tar",root_dir="dir2")
'/home/python/dir1.tar'
>>> shutil.make_archive("dir1","tar",root_dir="/home/python/dir2")
'/home/python/dir1.tar'
>>> shutil.make_archive("/home/python/dir2","tar",root_dir="dir1")#将dir1压缩成/home/pyhton/dir2
'/home/python/dir2.tar'
>>>
[root@store1 python]# tree
.
├── dir1
│ ├── 2.txt
│ └── 3.txt
├── dir1.tar
├── dir1.zip
├── dir2
│ └── 1.txt
└── dir2.tar 2 directories, 6 files
[root@store1 python]#
shutil 对压缩包的处理是调用 ZipFile 和 TarFile 两个模块来进行的
python基础--shutil模块的更多相关文章
- python基础——第三方模块
python基础——第三方模块 在Python中,安装第三方模块,是通过包管理工具pip完成的. 如果你正在使用Mac或Linux,安装pip本身这个步骤就可以跳过了. 如果你正在使用Window ...
- python基础——使用模块
python基础——使用模块 Python本身就内置了很多非常有用的模块,只要安装完毕,这些模块就可以立刻使用. 我们以内建的sys模块为例,编写一个hello的模块: #!/usr/bin/env ...
- 二十五. Python基础(25)--模块和包
二十五. Python基础(25)--模块和包 ● 知识框架 ● 模块的属性__name__ # my_module.py def fun1(): print("Hello& ...
- python 基础之 模块
Python 基础之模块 一个模块就是一个包含了python定义和声明的文件,文件名就是模块名字加上.py的后缀. 就是一个python文件中定义好了类和方法,实现了一些功能,可以被别的python文 ...
- Python 基础之模块之os os.path 及os与shutil对比
一: os 对系统进行操作 #注:以下操作都在linux环境下操作,且很多运行之前需要做好相关条件import os#(1)system() 在python总执行系统命令#os.system(&quo ...
- 周末班:Python基础之模块
什么是模块 什么是模块? 常见的场景:一个模块就是一个包含了python定义和声明的文件,文件名就是模块名字加上.py的后缀. 但其实import加载的模块分为四个通用类别: 1 使用python编写 ...
- python之shutil模块的使用
shutil模块 shutil模块是一种高级的文件操作工具,其对文件的复制与删除操作非常强大,shutil 名字来源于 shell utilities,该模块拥有许多文件(夹)操作的功能,包括复制.移 ...
- 学习PYTHON之路, DAY 6 - PYTHON 基础 6 (模块)
一 安装,导入模块 安装: pip3 install 模块名称 导入: import module from module.xx.xx import xx from module.xx.xx impo ...
- Python自动化 【第五篇】:Python基础-常用模块
目录 模块介绍 time和datetime模块 random os sys shutil json和pickle shelve xml处理 yaml处理 configparser hashlib re ...
随机推荐
- Python OS模块操作文件和目录
#-*-coding:utf-8-*- import os import shutil ###############OS模块############## #获得当前python脚本的工作目录 os. ...
- Go interface{}、类型断言
在 golang 中 interface{} 可用于向函数传递任意类型的变量, 但在函数内部使用的话, 该变量的类型就是 interface{}, 也称为空接口类型 比如我们定义一个函数, 输出字符串 ...
- oracle的学习笔记
Oracle的介绍 1. Oracle的创始人----拉里•埃里森 2. oracle的安装 [连接Oracle步骤](](https://img2018.cnblogs.com/blog/12245 ...
- 退出Android程序时清除所有activity的实现方法
思路: 1. 自定义ActivityList管理类,添加删除维护该list; 2.Activity Stack 类似上面: 3.singleTask定义一个Activity为该启动模式,然后当返回时, ...
- Chapter3 (字符串,向量,数组) --C++Prime笔记
1.using用法:using namespace ::name;注意事项:一般不在头文件使用using否则很容易导致运用命名空间不对错误. 2.string的方法: ①getline(输入流,str ...
- 在Kubernetes集群里安装微服务DevOps平台fabric8
转载于https://blog.csdn.net/wzp1986/article/details/72128063?utm_source=itdadao&utm_medium=referral ...
- js基础之DOM中document对象的常用属性方法
-----引入 每个载入浏览器的 HTML 文档都会成为 Document 对象. Document 对象使我们可以从脚本中对 HTML 页面中的所有元素进行访问. 属性 1 document.an ...
- [leetcode]multiply-strings java代码
题目: Given two numbers represented as strings, return multiplication of the numbers as a string. Note ...
- Hadoop生态圈-桶表和分区表
Hadoop生态圈-桶表和分区表 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任.
- 2017 清北济南考前刷题Day 7 afternoon
期望得分:100+100+30=230 实际得分:100+100+30=230 1. 三向城 题目描述 三向城是一个巨大的城市,之所以叫这个名字,是因为城市中遍布着数不尽的三岔路口.(来自取名力为0的 ...