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模块的更多相关文章

  1. python基础——第三方模块

    python基础——第三方模块 在Python中,安装第三方模块,是通过包管理工具pip完成的.  如果你正在使用Mac或Linux,安装pip本身这个步骤就可以跳过了.  如果你正在使用Window ...

  2. python基础——使用模块

    python基础——使用模块 Python本身就内置了很多非常有用的模块,只要安装完毕,这些模块就可以立刻使用. 我们以内建的sys模块为例,编写一个hello的模块: #!/usr/bin/env ...

  3. 二十五. Python基础(25)--模块和包

    二十五. Python基础(25)--模块和包 ● 知识框架   ● 模块的属性__name__ # my_module.py   def fun1():     print("Hello& ...

  4. python 基础之 模块

    Python 基础之模块 一个模块就是一个包含了python定义和声明的文件,文件名就是模块名字加上.py的后缀. 就是一个python文件中定义好了类和方法,实现了一些功能,可以被别的python文 ...

  5. Python 基础之模块之os os.path 及os与shutil对比

    一: os 对系统进行操作 #注:以下操作都在linux环境下操作,且很多运行之前需要做好相关条件import os#(1)system() 在python总执行系统命令#os.system(&quo ...

  6. 周末班:Python基础之模块

    什么是模块 什么是模块? 常见的场景:一个模块就是一个包含了python定义和声明的文件,文件名就是模块名字加上.py的后缀. 但其实import加载的模块分为四个通用类别: 1 使用python编写 ...

  7. python之shutil模块的使用

    shutil模块 shutil模块是一种高级的文件操作工具,其对文件的复制与删除操作非常强大,shutil 名字来源于 shell utilities,该模块拥有许多文件(夹)操作的功能,包括复制.移 ...

  8. 学习PYTHON之路, DAY 6 - PYTHON 基础 6 (模块)

    一 安装,导入模块 安装: pip3 install 模块名称 导入: import module from module.xx.xx import xx from module.xx.xx impo ...

  9. Python自动化 【第五篇】:Python基础-常用模块

    目录 模块介绍 time和datetime模块 random os sys shutil json和pickle shelve xml处理 yaml处理 configparser hashlib re ...

随机推荐

  1. 响应式开发(五)-----Bootstrap CSS----------Bootstrap 网格系统

    如果我们看过一些bootstrap的框架,经常看到col-sm-3等样式class. Bootstrap 提供了一套响应式.移动设备优先的流式网格系统,随着屏幕或视口(viewport)尺寸的增加,系 ...

  2. Android 资源目录 相关知识 raw、 drawable、 values..

    一定要看的 Android 资源目录的相关知识 raw drwable valueshttp://blog.csdn.net/shichexixi/article/details/5985677 ht ...

  3. 解题:HEOI 2016 求和

    题面 我们需要知道这样一个东西(大概叫 斯特林公式?) $S(i,j)=\frac{1}{j!}\sum\limits_{k=0}^{j}(-1)^k C_j^k(j-k)^i$ 那么就是推啊 $=\ ...

  4. 【cf859E】Desk Disorder

    Portal --> cf859E Solution ​​ 我们可以将每一个人看成一条边,将位置看成点,然后一个人在新的方案中可以选择的位置就是这条边连接的两个点,然后我们就得到了一个图 ​ 注 ...

  5. Docker入门与应用系列(二)镜像管理

    1.1 什么是镜像 简单说,Docker镜像是一个不包含Linux内核而又精简的Linux操作系统. 1.2 镜像从哪里来 Docker Hub是由Docker公司负责维护的公共注册中心,包含大量的容 ...

  6. 「Vue」程序式路由导航用法

    1.button发起点击请求<mt-button type='primary' size='large' plain @click="topdcmt(id)">商品评论 ...

  7. filebeat过滤

    合并多行以[为头 multiline:pattern: '^\['negate: truematch: after ------------------------------------------ ...

  8. linq的语法和案例

    本篇逐一介绍linq各个关键字的用法(from,select,group,into等),本篇所有的案例都是用linqpad来完成的(官方地址:http://www.linqpad.net/),建议想学 ...

  9. 实现asp.net的文件压缩、解压、下载

    很早前就想做文件的解压.压缩.下载 了,不过一直没时间,现在项目做完了,今天弄了下.不过解压,压缩的方法还是看的网上的,嘻嘻~~不过我把它们综合了一下哦.呵呵~~ 1.先要从网上下载一个icsharp ...

  10. windows环境下git的环境变量配置

    1.从官网下载git这个软件.msi格式,然后安装. 2.找到你的git的安装目录,并记录下来 3.配置环境变量:在path里加入——  ;你的git的安装目录\bin;你的git的安装目录\libe ...