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 ...
随机推荐
- 单点登录(十二)-----遇到问题-----cas启用mongodb验证方式登录后没反应-pac4j-mongo包中的MongoAuthenticatInvocationTargetException
cas启用mongodb验证方式登录后没反应 控制台输出 2017-02-09 20:27:15,766 INFO [org.jasig.cas.authentication.MongoAuthent ...
- YY淘宝商品数据库设计
http://www.cnblogs.com/mmmjiang13/archive/2010/11/04/1868609.html 前言 这几个月都在做一个通过淘宝API线下管理淘宝店的系统,学习了很 ...
- Memcache PHP 使用笔记
Memcache PHP 使用笔记 最近在做网站迁移 看到之前的一个网站目录下Cache文件里上万的缓存文件真是害怕 新的服务器上配置了memcache扩展 于是乎准备折腾一下看看能不能把之前的文件缓 ...
- php配置修改后,平滑启动php-fpm
修改了php配置需要平滑启动php-fpm ps -aux | grep php-fpm 找到phpfpm 的master process的进程id kill -SIGUSR2 31158 实现平 ...
- 使用Java API的5个技巧
原文出处:CSDN邓帅 本文介绍了一些关于Java API安全和性能方面的简单易用的技巧,其中包括保证API Key安全和开发Web Service方面中在框架方面选择的一些建议. 程序员都喜欢使用A ...
- ROI align解释
转自:blog.leanote.com/post/afanti.deng@gmail.com/b5f4f526490b ROI Align 是在Mask-RCNN这篇论文里提出的一种区域特征聚集方式, ...
- Hadoop生态圈-使用MapReduce处理HBase数据
Hadoop生态圈-使用MapReduce处理HBase数据 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.对HBase表中数据进行单词统计(TableInputFormat) ...
- JAVA-JSP动作
动作元素基本上是预定义的功能.下表列出了可用的JSP动作 - 编号 动作 描述 1 jsp:include 在请求页面时包含一个文件. 2 jsp:useBean 查找或实例化一个JavaBean. ...
- Mongodb 笔记04 特殊索引和集合、聚合、应用程序设计
特殊索引和集合 1. 固定集合:固定集合需要事先创建好看,而且它的大小是固定的.当固定集合被占满时,如果再插入新文档,固定集合会自动将最老的文档从集合中删除. 2. 创建固定集合:db.createC ...
- jdk源码
Java 8 之默认方法(Default Methods) public interface Player { String getName(); default boolean isMale() { ...