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. 安装GourdScanV2的踩坑过程

    环境:ubuntu 16.04.1 1.安装dcoker sudo apt-get install docker.io 坑:sudo apt-get install docker 2.下载关于dock ...

  2. phpredis -- Redis Arrays用法

    Redis Arrays 来自地址:https://github.com/phpredis/phpredis/blob/master/arrays.markdown#readme 扩展原文件array ...

  3. 关于nodejs的几个干货(读中文文件编码问题/发送邮件/定时任务)

    关于nodejs读取中文文件真是折腾了不少时间,网上各种方案,最后没有一个适用我,好在解决了. 下面的三个知识点都是从项目中抽出的,要单独运行脚本的话需要用全局模式来安装模块,比如安装中文转换模块(后 ...

  4. HDU--4768

    题目: Flyer 原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4768 分析:二分.只需要注意到最多只有一个为奇数,则可以首先求出学生获得的总的传单 ...

  5. 「Vue」自定义指令

    #全局自定义指令1.使用Vue.directive()定义全局的指令 v-focus2.参数1 指令的名称,在定义的时候,指令的名称前面不需要加v-前缀3.但是在调用的时候必须在指令名称前 加上v-前 ...

  6. Hadoop基础-Hadoop快照管理

    Hadoop基础-Hadoop快照管理 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.快照的作用 快照可以迅速对文件(夹)进行备份,不产生新文件,使用差值存储,默认是禁用状态. ...

  7. NO.11天作业

    打印uid在30~40范围内的用户名.awk -F: '$3>=30 && $3<=40{print $1,$3}' /etc/passwd 打印第5-10行的行号和用户名 ...

  8. 使用object_box遇到的崩溃 java.lang.UnsatisfiedLinkError:

    java.lang.UnsatisfiedLinkError: dalvik.system.PathClassLoader[DexPathList[[zip file "/data/app/ ...

  9. JAVA 急速WEB框架Blast-本人开发的JavaWeb急速框架Blast上线了

    JAVA 急速WEB框架Blast ——对JavaWeb的学习性框架,参考了spring的实现 ——阅读Blast源码可以快速掌握JavaWeb常用技术和方法论,并付诸实践 Blast 是基于 Jav ...

  10. jenkins 相关默认信息

    (1) 默认安装目录    /usr/lib/jenkins/:jenkins安装目录,WAR包会放在这里.  ( 2 )  默认配置文件 /etc/sysconfig/jenkins:jenkins ...