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的当前文件开始

  1. import shutil
  2.  
  3. r_file=open("test1.py",'r',encoding="utf-8")
  4. w_file=open("test22.py",'w',encoding="utf-8")
  5. 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的是字符串形式的路径名

例子如下:

  1. shutil.copyfile("test1.py","test2-2.py")

3)shutil.copymode(src, dst)
仅拷贝权限。内容、组、用户均不变

  1. root@store1 python]# cat 1.txt
  2. 1111111111122222222
  3. [root@store1 python]# python
  4. Python 3.6.0 (default, Feb 16 2017, 20:26:37)
  5. [GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux
  6. Type "help", "copyright", "credits" or "license" for more information.
  7. >>> import shutil
  8. >>> shutil.copy("1.txt","2.txt")
  9. '2.txt'
  10. >>> exit
  11.  
  12. [root@store1 python]# ls -l
  13. total 8
  14. -rw-r--r--. 1 root root 20 May 13 21:16 1.txt
  15. -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时间同步

  1. >>> shutil.copystat("1.txt","2.txt")
  2. >>>
  3. [root@store1 python]# ls -l
  4. total 8
  5. -rw-r--r--. 1 root root 20 May 13 21:16 1.txt
  6. -rw-r--r--. 1 root root 20 May 13 21:16 2.txt

5)shutil.copy(src, dst)
拷贝文件和权限。调用copyfile(src, dst)和copymode(src, dst)

  1. [root@store1 python]# python
  2. Python 3.6.0 (default, Feb 16 2017, 20:26:37)
  3. [GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux
  4. Type "help", "copyright", "credits" or "license" for more information.
  5. >>> import shutil
  6. >>> shutil.copy("1.txt","3.txt")
  7. '3.txt'
  8. >>>
  9. [root@store1 python]# ls -l
  10. total 12
  11. -rw-r--r--. 1 root root 20 May 13 21:16 1.txt
  12. -rw-r--r--. 1 root root 20 May 13 21:16 2.txt
  13. -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)
递归的去拷贝文件

  1. drwxr-xr-x. 2 root root 45 May 13 21:38 dir1
  2. [root@store1 python]# python
  3. Python 3.6.0 (default, Feb 16 2017, 20:26:37)
  4. [GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux
  5. Type "help", "copyright", "credits" or "license" for more information.
  6. >>> import shutil
  7. >>> shutil.copytree("dir1","dir2")
  8. 'dir2'
  9. >>>
  10. [root@store1 python]# ls -R
  11. .:
  12. dir1 dir2
  13.  
  14. ./dir1:
  15. 1.txt 2.txt 3.txt
  16.  
  17. ./dir2:
  18. 1.txt 2.txt 3.txt
  19. [root@store1 python]#

8)shutil.rmtree(path[, ignore_errors[, onerror]])
递归的去删除文件

  1. [root@store1 python]# tree
  2. .
  3. ├── dir1
  4.    ├── 1.txt
  5.    ├── 2.txt
  6.    └── 3.txt
  7. └── dir2
  8. ├── 1.txt
  9. ├── 2.txt
  10. └── 3.txt
  11.  
  12. 2 directories, 6 files
  13. [root@store1 python]# python
  14. Python 3.6.0 (default, Feb 16 2017, 20:26:37)
  15. [GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux
  16. Type "help", "copyright", "credits" or "license" for more information.
  17. >>> import shutil
  18. >>> shutil.rmtree("dir2")
  19. >>>
  20. [root@store1 python]# tree
  21. .
  22. └── dir1
  23. ├── 1.txt
  24. ├── 2.txt
  25. └── 3.txt
  26.  
  27. 1 directory, 3 files
  28. [root@store1 python]#

9)shutil.move(src, dst)
递归的去移动文件

  1. [root@store1 python]# tree
  2. .
  3. ├── dir1
  4.    ├── 1.txt
  5.    ├── 2.txt
  6.    └── 3.txt
  7. └── dir2
  8.  
  9. 2 directories, 3 files
  10. [root@store1 python]# python
  11. Python 3.6.0 (default, Feb 16 2017, 20:26:37)
  12. [GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux
  13. Type "help", "copyright", "credits" or "license" for more information.
  14. >>> import shutil
  15. >>> shutil.move("dir1/1.txt","dir2")
  16. 'dir2/1.txt'
  17. >>>
  18. [root@store1 python]# tree
  19. .
  20. ├── dir1
  21.    ├── 2.txt
  22.    └── 3.txt
  23. └── dir2
  24. └── 1.txt
  25.  
  26. 2 directories, 3 files
  27. [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对象

  1. [root@store1 python]# tree
  2. .
  3. ├── dir1
  4.    ├── 2.txt
  5.    └── 3.txt
  6. └── dir2
  7. └── 1.txt
  8.  
  9. 2 directories, 3 files
  10. [root@store1 python]# python
  11. Python 3.6.0 (default, Feb 16 2017, 20:26:37)
  12. [GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux
  13. Type "help", "copyright", "credits" or "license" for more information.
  14. >>> import shutil
  15. >>> shutil.make_archive("dir1","zip")
  16. 'dir1.zip'
  17. >>> shutil.make_archive("dir1","tar",root_dir="dir2")
  18. '/home/python/dir1.tar'
  19. >>> shutil.make_archive("dir1","tar",root_dir="/home/python/dir2")
  20. '/home/python/dir1.tar'
  21. >>> shutil.make_archive("/home/python/dir2","tar",root_dir="dir1")#将dir1压缩成/home/pyhton/dir2
  22. '/home/python/dir2.tar'
  23. >>>
  24. [root@store1 python]# tree
  25. .
  26. ├── dir1
  27.    ├── 2.txt
  28.    └── 3.txt
  29. ├── dir1.tar
  30. ├── dir1.zip
  31. ├── dir2
  32.    └── 1.txt
  33. └── dir2.tar
  34.  
  35. 2 directories, 6 files
  36. [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. bzoj4427【Nwerc2015】Cleaning Pipes清理管道

    题目描述 Linköping有一个相当复杂的水资源运输系统.在Linköping周围的出水点有一些水井.这些水通过管道输送到其它地点.每条管道是从某一个水井到城市的某个位置的直线管道. 所有管道在地下 ...

  2. go数据类型之基本类型

    基本数据类型 数值型 整数 int 64位操作系统默认为int64,32位操作系统为int32,但是类型检查时时int int8 (byte 1字节) int16 (short 2字节) int32 ...

  3. laravel DB listen 回调追踪产生 sql 语句的代码

    \DB::listen(function (QueryExecuted $sql) { \Log::info($sql->sql); \Log::info((new \Exception())- ...

  4. STM32 ------ HardFault_Hander 中断函数

    1.数组越界(对数组赋值) 正常情况,数组越界会进入 HardFault_Hander 中断函数的无线循环. 避免数组越界的一个方法是:每次使用数组前,检查要放入数据的数据长度是否大于数组长度,大于则 ...

  5. DHCP 服务器功能

    DHCP服务器不仅可以分配IP地址,同时也可以分配网关和DNS服务器地址

  6. Python【异常处理】

    def f(): first = input('请输入除数:') second = input('请输入被除数:') try: first = int(first) second = int(seco ...

  7. 使用docker配置etcd集群

    docker配置etcd集群与直接部署etcd集群在配置上并没有什么太大差别. 我这里直接使用docker-compose来实现容器化的etcd部署 环境如下: HostName IP etcd1 1 ...

  8. ROI align解释

    转自:blog.leanote.com/post/afanti.deng@gmail.com/b5f4f526490b ROI Align 是在Mask-RCNN这篇论文里提出的一种区域特征聚集方式, ...

  9. Hadoop部署方式-本地模式(Local (Standalone) Mode)

    Hadoop部署方式-本地模式(Local (Standalone) Mode) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. Hadoop总共有三种运行方式.本地模式(Local ...

  10. python3使用stmplib发送邮件

    代码如下: import smtplib from email.mime.text import MIMEText from email.header import Header from email ...