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. v4l2的学习建议和流程解析

    v4l2,一开始听到这个名词的时候,以为又是一个很难很难的模块,涉及到视频的处理,后来在网上各种找资料后,才发现其实v4l2已经分装好了驱动程序,只要我们根据需要调用相应的接口和函数,从而实现视频的获 ...

  2. Spring Boot CRUD+分页(基于Mybatis注解方式)

    步骤一:关于Mybatis Mybatis 是用来进行数据库操作的框架.其中分页使用Mybatis中的PageHelper插件. Mybatis与hibernate对比: 1.hibernate是一个 ...

  3. TeeChart.Direct2D.dll的使用

    这个dll本身依赖于第三方的控件,SlimDX ,可以从 http://slimdx.org/ 下载.  .net4.0的版本区分x86和x64 帧数的概念 我们通常说帧数,简单地说,就是在1秒钟时间 ...

  4. LA 3720 高速公路(互质判斜率)

    https://vjudge.net/problem/UVALive-3720 题意: 有一个n行m列的点阵,问一共有多少条非水平非垂直的直线至少穿过其中的两个点. 思路: 没思路的题. 首先枚举矩形 ...

  5. 好的Mysql 查询语句

    select swr.id,swr.name,swr.sort as type,count(swl.id) as nums,ifnull(sum(swl.package_num),0) package ...

  6. ADC第一次读取

    在ADCCON中,最后0位和1位互斥.如果1位选1的话,0位的值无效.如果1位选0的话,0位的值才有效.当1位选1的话:这是应用层的程序 #if ADSTART==0void niuniu(void) ...

  7. mina-deploy(3800🌟) 快速部署工具

    Mina  (3800

  8. Mysql的alter用法

    一.在已有表上创建索引  1.ALTER TABLE <表名> ADD PRIMARY KEY (字段名); ALTER TABLE <表名> DROP PRIMARY KEY ...

  9. python进行linux系统监控

      python进行linux系统监控 Linux系统下: 静态指标信息: 名称 描述 单位 所在文件 mem_total 内存总容量 KB /proc/meminfo disks 磁盘相关信息 - ...

  10. mail_location not set and autodetection failed 解决方案[devecot, sendmail]

    安装dovecot比较简单, 但是也需要配置, 如果不进行任何配置时,在测试时会出现如下的提示: dovecot: pop3(wwufengg): Error: user wwufengg: Init ...