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. [转]C语言 gets()和scanf()函数的区别

    scanf( )函数和gets( )函数都可用于输入字符串,但在功能上有区别.若想从键盘上输入字符串"hi hello",则应该使用__gets__函数. gets可以接收空格:而 ...

  2. CSS 属性的推荐书写顺序

  3. Job流程:Mapper类分析

    此文紧接Job流程:决定map个数的因素,Map任务被提交到Yarn后,被ApplicationMaster启动,任务的形式是YarnChild进程,在其中会执行MapTask的run()方法.无论是 ...

  4. openwrt如何查看当前使用的硬件平台

    答:输入cat /tmp/sysinfo/board_name即可获取

  5. [BZOJ3733]Iloczyn

    Description 给定正整数n和k,问能否将n分解为k个不同正整数的乘积 Input 第一行一个数T(T<=4000)表示测试组数 接下来T行每行两个数n(n<=10^9),k(k& ...

  6. swift学习笔记 - 判断当前运行的系统和平台

    最近代码需要判断代码运行的系统与平台,下面总结了一下swift下一些可以用来判断的属性: // 代码运行在32位的 Windows public var TARGET_OS_MAC: Int32 { ...

  7. Mac OS build caffe2 Error:This file was generated by an older version of protoc which is

    问题所在 我们可以发现这个错误跟protobuf的版本有关,因此我们可以执行script/diagnose_protobuf.py 我们可以看到,pip install protobuf 和 brew ...

  8. Web前端可以转行做游戏吗?

    作者:ManfredHu 链接:http://www.manfredhu.com/2018/03/15/31-laya-game-tips/index.html 声明:版权所有,转载请保留本段信息,谢 ...

  9. Memcached flush_all 命令

    Memcached flush_all 命令用于用于清理缓存中的所有 key=>value(键=>值) 对. 该命令提供了一个可选参数 time,用于在制定的时间后执行清理缓存操作. 语法 ...

  10. Servlet之监听事件细究

    观察者三个模式: ServletContextListener:用于监听WEB 应用启动和销毁的事件,监听器类需要实现javax.servlet.ServletContextListener 接口. ...