欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

python常用模块之shutil模块

程序员文章站 2022-07-23 12:57:05
python常用模块之shutil模块 shutil模块,高级的文件、文件夹、压缩包处理模块 1. :将文件内容拷贝到另一个文件 2. :拷贝文件,目标文件无需存在 3. :仅拷贝权限。内容、组、用户均不变,目标文件需要存在 4. :仅拷贝文件状态的信息,包括:mode bits atims mti ......

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目录下了。