python常用模块之shutil模块

shutil模块,高级的文件、文件夹、压缩包处理模块

1.shutil.copyfile(src,des[,length]):将文件内容拷贝到另一个文件

In [1]: import os  # 导入os模块

In [2]: os.listdir()  # 查看当前目录下的文件和目录
Out[2]:
['.XIM-unix',
'.X11-unix',
'.font-unix',
'.Test-unix',
'.ICE-unix',
'replaceContent.py'] In [3]: import shutil # 导入shutil模块 In [4]: shutil.copyfileobj(open('replaceContent.py','r'),open('new_scripts.py','w')) # 以读的形式打开旧文件,再以写的形式写入新文件 In [5]: os.listdir()
Out[5]:
['.XIM-unix',
'.X11-unix',
'.font-unix',
'.Test-unix',
'.ICE-unix',
'replaceContent.py',
'new_scripts.py']

2.shutil.copyfile(src,des):拷贝文件,目标文件无需存在

In [8]: shutil.copyfile("new_scripts.py","scripts.py")  # 把旧文件拷贝成为新文件
Out[8]: 'scripts.py' In [9]: os.listdir()
Out[9]:
['.XIM-unix',
'.X11-unix',
'.font-unix',
'.Test-unix',
'.ICE-unix',
'replaceContent.py',
'new_scripts.py',
'scripts.py'] # 内容和new_scripts.py一样

3.shutil.copymode(src,des):仅拷贝权限。内容、组、用户均不变,目标文件需要存在

# 现在当前目录创建test.py文件,并且将scripts.py权限修改为755
In [11]: shutil.copymode("scripts.py","test.py")
代码运行如下:
[root@host-10-200-137-195 tmp]# ll
total 12
-rw-r--r-- 1 root root 1149 May 9 17:21 new_scripts.py
-rw-r--r-- 1 root root 1149 May 8 09:47 replaceContent.py
-rwxr-xr-x 1 root root 1149 May 9 17:24 scripts.py
-rwxr-xr-x 1 root root 0 May 9 17:26 test.py # 权限相同,但是内容没有复制过来,只复制权限

4.shutil.copystat():仅拷贝文件状态的信息,包括:mode bits atims mtime flags,目标文件必须存在

In [12]: shutil.copystat("scripts.py","test.py")
代码运行如下:
-rwxr-xr-x 1 root root 1149 May 9 17:24 scripts.py
-rwxr-xr-x 1 root root 0 May 9 17:24 test.py # 除了文件内容不一样,其余都是差不多的

5.shutil.copy(src,des):拷贝文件和权限

In [13]: shutil.copy("scripts.py","test.py")
Out[13]: 'test.py'
代码运行如下:
total 16
-rw-r--r-- 1 root root 1149 May 9 17:21 new_scripts.py
-rw-r--r-- 1 root root 1149 May 8 09:47 replaceContent.py
-rwxr-xr-x 1 root root 1149 May 9 17:24 scripts.py
-rwxr-xr-x 1 root root 1149 May 9 17:33 test.py # 只拷贝了文件内容和权限

6.shutil.copy2():拷贝文件和状态信息

# 此时删除test.py文件
In [14]: shutil.copy2("scripts.py","test.py")
Out[14]: 'test.py'
代码运行如下:
[root@host-10-200-137-195 tmp]# ll
total 16
-rw-r--r-- 1 root root 1149 May 9 17:21 new_scripts.py
-rw-r--r-- 1 root root 1149 May 8 09:47 replaceContent.py
-rwxr-xr-x 1 root root 1149 May 9 17:24 scripts.py
-rwxr-xr-x 1 root root 1149 May 9 17:24 test.py

7.shutil.copytree(src,des):递归的去拷贝文件夹,目标目录不能存在

# 首先在/tmp目录下创建a.txt文件
In [19]: shutil.copytree('/tmp/','/a/b/c/',ignore = shutil.ignore_patterns("*.py"))
Out[19]: '/a/b/c/'
代码运行如下:
[root@host-10-200-137-195 c]# pwd
/a/b/c
[root@host-10-200-137-195 c]# ls
a.txt
# 要对/a/b/c目录有写权限
# ignore代表排除

8.shutil.retree(src):递归的删除文件

In [20]: shutil.rmtree('/a/b/c/')  # 只需一条命令,就删除了目录

9.shutil.move(src,des):递归的去移动文件,它类似mv命令,其实就是重命名

In [21]: shutil.move('test.py','new_test.py')
Out[21]: 'new_test.py' In [22]: ll
total 16
-rw-r--r-- 1 root 0 May 9 17:39 a.txt
-rw-r--r-- 1 root 1149 May 9 17:21 new_scripts.py
-rwxr-xr-x 1 root 1149 May 9 17:24 new_test.py*
-rw-r--r-- 1 root 1149 May 8 09:47 replaceContent.py
-rwxr-xr-x 1 root 1149 May 9 17:24 scripts.py*

10.shutil.make_archive(base_name,format,root_dir):创建压缩包并返回文件路径

  • base_name:压缩包的文件名,也可以是压缩包的路径。只是文件名时,则保存到当前目录,否则保存刀片指定的目录
  • format:压缩包种类,例如:"zip","tar","bztar","gztar"
  • root_dir:要压缩的文件夹路径(默认当前目录)
  • owner:用户,默认当前用户
  • group:组,默认当前组
  • logger:用于记录日志,通常时logging.Logger对象

举例看一下:

# 将/tmp下的文件打包放置当前程序目录,压缩包名为yasuo
In [26]: shutil.make_archive("yasuo",'zip','/tmp')
Out[26]: '/tmp/yasuo.zip'
代码运行结果:
[root@host-10-200-137-195 tmp]# ll
total 24
-rw-r--r-- 1 root root 0 May 9 17:39 a.txt
-rw-r--r-- 1 root root 1149 May 9 17:21 new_scripts.py
-rwxr-xr-x 1 root root 1149 May 9 17:24 new_test.py
-rw-r--r-- 1 root root 1149 May 8 09:47 replaceContent.py
-rwxr-xr-x 1 root root 1149 May 9 17:24 scripts.py
-rw-r--r-- 1 root root 4950 May 9 17:59 yasuo.zip
[root@host-10-200-137-195 tmp]# unzip yasuo.zip # 文件已经存在,解压问是否覆盖?
Archive: yasuo.zip
replace replaceContent.py? [y]es, [n]o, [A]ll, [N]one, [r]ename: y
inflating: replaceContent.py
replace new_scripts.py? [y]es, [n]o, [A]ll, [N]one, [r]ename: y
inflating: new_scripts.py
replace scripts.py? [y]es, [n]o, [A]ll, [N]one, [r]ename: y
inflating: scripts.py
replace a.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: y
inflating: a.txt
replace new_test.py? [y]es, [n]o, [A]ll, [N]one, [r]ename: y
inflating: new_test.py
replace yasuo.zip? [y]es, [n]o, [A]ll, [N]one, [r]ename: y
inflating: yasuo.zip
[root@host-10-200-137-195 tmp]# ll
total 20
-rw-r--r-- 1 root root 0 May 9 17:39 a.txt
-rw-r--r-- 1 root root 1149 May 9 17:21 new_scripts.py
-rwxr-xr-x 1 root root 1149 May 9 17:24 new_test.py
-rw-r--r-- 1 root root 1149 May 8 09:47 replaceContent.py
-rwxr-xr-x 1 root root 1149 May 9 17:24 scripts.py
-rw-r--r-- 1 root root 3319 May 9 17:59 yasuo.zip 我们也可以将/tmp目录下的文件打包放在/opt目录下重新命名:
In [29]: shutil.make_archive("/opt/aaa",'gztar','/tmp')
Out[29]: '/opt/aaa.tar.gz'
运行结果:
[root@host-10-200-137-195 opt]# ll
total 4
-rw-r--r-- 1 root root 2120 May 9 18:04 aaa.tar.gz
[root@host-10-200-137-195 opt]# ll -a
total 8
drwxr-xr-x. 2 root root 23 May 9 18:04 .
dr-xr-xr-x. 19 root root 4096 May 9 18:03 ..
-rw-r--r-- 1 root root 2120 May 9 18:04 aaa.tar.gz
[root@host-10-200-137-195 opt]# tar -zxvf aaa.tar.gz
./
./.XIM-unix/
./.X11-unix/
./.font-unix/
./.Test-unix/
./.ICE-unix/
./replaceContent.py
./new_scripts.py
./scripts.py
./a.txt
./new_test.py
./yasuo.zip

shutil对压缩包的处理是通过ZipFile和TarFile两个模块进行的,详细:

zipfile 压缩&解压缩


压缩:
In [1]: import zipfile # 导入这个模块 In [2]: z = zipfile.ZipFile("压缩.zip",'w') In [3]: z.write('a.txt') # 写入的内容应该是文件名 In [4]: z.write('new_test.py') In [5]: z.close() # 关闭
此时运行完毕后,会在/tmp 的目录下生成"压缩.zip"文件 解压缩:先删除a.txt和new_test.py文件
In [10]: z = zipfile.ZipFile('压缩.zip','r') In [12]: z.extractall('.') # 表示在当前目录下完成解压 In [13]: z.close()
那么在关闭的时候,文件就已经都回来了。不信你看:
-rw-r--r-- 1 root root 0 May 9 18:14 a.txt
-rw-r--r-- 1 root root 1149 May 9 17:21 new_scripts.py
-rw-r--r-- 1 root root 1149 May 9 18:14 new_test.py
-rw-r--r-- 1 root root 1149 May 8 09:47 replaceContent.py
-rwxr-xr-x 1 root root 1149 May 9 17:24 scripts.py
-rw-r--r-- 1 root root 1355 May 9 18:10 压缩.zip

tarfile压缩和解压缩

压缩:
In [1]: import tarfile In [2]: t = tarfile.open("/opt/yasuo.tar",'w') In [3]: t.add('a.txt',arcname="a.txt.bak") # 把a.txt.bak文件放到yasuo.tar进行压缩 In [4]: t.close()
代码运行完,就在/opt的目录下创建了yasuo.tar文件了 解压缩:
In [6]: t = tarfile.open('/opt/yasuo.tar','r') In [7]: t.extractall('/tmp') # 将它解压到/tmp目录下 In [8]: t.close()
此时,a.txt.tar已经回到/tmp目录下了。

python常用模块之shutil模块的更多相关文章

  1. [转]python中对文件、文件夹的操作——os模块和shutil模块常用说明

    转至:http://l90z11.blog.163.com/blog/static/187389042201312153318389/ python中对文件.文件夹的操作需要涉及到os模块和shuti ...

  2. Python中os和shutil模块实用方法集…

    Python中os和shutil模块实用方法集锦 类型:转载 时间:2014-05-13 这篇文章主要介绍了Python中os和shutil模块实用方法集锦,需要的朋友可以参考下 复制代码代码如下: ...

  3. Python中os和shutil模块实用方法集锦

    Python中os和shutil模块实用方法集锦 类型:转载 时间:2014-05-13 这篇文章主要介绍了Python中os和shutil模块实用方法集锦,需要的朋友可以参考下 复制代码代码如下: ...

  4. Python常用内置模块之xml模块

    xml即可扩展标记语言,它可以用来标记数据.定义数据类型,是一种允许用户对自己的标记语言进行定义的源语言.从结构上,很像HTML超文本标记语言.但他们被设计的目的是不同的,超文本标记语言被设计用来显示 ...

  5. os模块和shutil模块

    # coding=utf-8 import os path="D:\\test" ######### 目录结构如下 # test # / \ \ \ # test01 test02 ...

  6. 元数据的概念以及相关的操作os模块、shutil模块

    查看文件的元数据 stat [OPTION]… FILE… OPTION: -f 输出文件系统的状态,而非文件的状态 -t 显示简要格式的文件元数据信息 FILE:可同时查看多个文件的元数据信息,多个 ...

  7. Python进阶5---StringIO和BytesIO、路径操作、OS模块、shutil模块

    StringIO StringIO操作 BytesIO BytesIO操作 file-like对象 路径操作 路径操作模块 3.4版本之前:os.path模块 3.4版本开始 建议使用pathlib模 ...

  8. python之模块之shutil模块

    shutil -- --High-level file operations  高级的文件操作模块. os模块提供了对目录或者文件的新建/删除/查看文件属性,还提供了对文件以及目录的路径操作.比如说: ...

  9. Python入门-模块2(sys模块、shutil 模块)

    sys模块: sys.argv 命令行参数List,第一个元素是程序本身路径 sys.exit(n) 退出程序,正常退出时exit(0) sys.version 获取Python解释程序的版本信息 s ...

随机推荐

  1. Jquery2 基础核心

    学习要点: 1.代码风格 2.加载模式 3.对象互换 4.多个库之间的冲突 本节简单的介绍一下jQuery 一些核心的问题. 一.代码风格 在jQuery程序中,不管是页面元素的选择.内置的功能函数, ...

  2. [转][修]C清空输入缓冲区

    为何要清空输入缓存区     读取时输入缓冲区中的内容会被scanf函数逐个取走,正常case下scanf()函数可以根据返回值判断成功取走的数目:但当发生读取异常之后,输入缓冲区中的内容并未被取走, ...

  3. elasticsearch系列(六)备份

    快照备份 1.创建文件仓库 1.1 在$ELASTICSEARCH_HOME/config/elasticsearch.yaml中增加配置 #这个路径elasticsearch必须有权限访问,这个路径 ...

  4. Kruskal算法初步

    2017-09-18 21:53:00 writer:pprp 代码如下: /* @theme: kruskal @writer:pprp @date:2017/8/19 @begin:21:19 @ ...

  5. Flume NG初次使用

    一.什么是Flume NG Flume是一个分布式.可靠.和高可用性的海量日志采集.聚合和传输的系统,支持在日志系统中定制各类数据发送方,用于收集数据:同时Flume提供对数据的简单处理,并写到各种数 ...

  6. [异常记录(三)] 从 bcp 客户端收到一个对 colid 12 无效的列长度

    这个问题是使用SqlBulkCopy拷贝数据,字符串长度超出数据类型长度导致的. 处理过程中对长度进行判断并截取就OK了. *注:SqlBulkCopy 这货 要求ColumnMappings 列的大 ...

  7. LA 3523 圆桌骑士(二分图染色+点双连通分量)

    https://vjudge.net/problem/UVALive-3523 题意: 有n个骑士经常举行圆桌会议,商讨大事.每次圆桌会议至少应有3个骑士参加,且相互憎恨的骑士不能坐在圆桌旁的相邻位置 ...

  8. Kubernetes 删除 namespace

    一. 正常情况情况下的删除: kubectl delete namespace jenkins 二. 如果上面的方法不能删除,且namespace的状态一直显示为Terminating的话 要查看一下 ...

  9. 删除Rancher节点的正确姿势

    在Rancher上疏散该节点 删除节点 登录该节点宿主机,删除rancher相关容器 docker rm -f -v $(docker ps -aq) 删除该节点的所有volume docker vo ...

  10. 百度编辑器(ueditor)@功能之获取坐标

    //获取百度编辑器的工具类 var domUtils = UE.dom.domUtils; //获取编辑器的坐标 var $ueditor_offset = $("#ueditor_0&qu ...