shutil模块

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

常用方法

1、shutil.copyfileobj(fsrc, fdst[, length]) --将文件内容拷贝到另一个文件中

>>> import shutil
>>> shutil.copyfileobj(open('fileA.txt','r'), open('fileB.txt','w'))

2、shutil.copyfile(src, dst) --拷贝文件

>>> import shutil
>>> shutil.copyfile('fileA.txt','fileB.txt')
'fileB.txt'

3、shutil.copymode(src, dst) --仅拷贝权限。内容、组、用户均不变

初始权限:
linux-ko5m:/home/python/test # ll
总用量 8
-rw-r--r-- 1 root root 105 5月 18 23:13 fileA.txt
---------- 1 root root 105 5月 18 23:20 fileB.txt

执行:
>>> shutil.copymode('fileA.txt','fileB.txt')

结果:
linux-ko5m:/home/python/test # ll
总用量 8
-rw-r--r-- 1 root root 105 5月 18 23:13 fileA.txt
-rw-r--r-- 1 root root 105 5月 18 23:20 fileB.txt

4、shutil.copystat(src, dst) --仅拷贝状态的信息,包括:mode bits, atime, mtime, flag

初始:
linux-ko5m:/home/python/test # stat *
文件:"fileA.txt"
大小:105 块:8 IO 块:4096 普通文件
设备:32h/50d Inode:387 硬链接:1
权限:(0644/-rw-r--r--) Uid:( 0/ root) Gid:( 0/ root)
最近访问:2020-05-18 23:14:00.865010232 +0800
最近更改:2020-05-18 23:13:58.672984945 +0800
最近改动:2020-05-18 23:13:58.672984945 +0800
创建时间:-
文件:"fileB.txt"
大小:105 块:8 IO 块:4096 普通文件
设备:32h/50d Inode:390 硬链接:1
权限:(0644/-rw-r--r--) Uid:( 0/ root) Gid:( 0/ root)
最近访问:2020-05-18 23:20:20.976598184 +0800
最近更改:2020-05-18 23:20:16.480562975 +0800
最近改动:2020-05-18 23:23:05.066532066 +0800
创建时间:-

执行:
>>> shutil.copystat('fileA.txt','fileB.txt')

结果:
linux-ko5m:/home/python/test # stat *
文件:"fileA.txt"
大小:105 块:8 IO 块:4096 普通文件
设备:32h/50d Inode:387 硬链接:1
权限:(0644/-rw-r--r--) Uid:( 0/ root) Gid:( 0/ root)
最近访问:2020-05-18 23:14:00.865010232 +0800
最近更改:2020-05-18 23:13:58.672984945 +0800
最近改动:2020-05-18 23:13:58.672984945 +0800
创建时间:-
文件:"fileB.txt"
大小:105 块:8 IO 块:4096 普通文件
设备:32h/50d Inode:390 硬链接:1
权限:(0644/-rw-r--r--) Uid:( 0/ root) Gid:( 0/ root)
最近访问:2020-05-18 23:14:00.865010232 +0800
最近更改:2020-05-18 23:13:58.672984945 +0800
最近改动:2020-05-18 23:25:25.636249508 +0800
创建时间:-

5、shutil.copy(src, dst) --拷贝文件和权限

>>> shutil.copy('fileA.txt','fileC.txt')
'fileC.txt'

结果:
linux-ko5m:/home/python/test # ll
总用量 8
-rw-r--r-- 1 root root 105 5月 18 23:13 fileA.txt
-rw-r--r-- 1 root root 105 5月 18 23:39 fileC.txt

6、shutil.copy2(src, dst) --拷贝文件和状态信息

>>> shutil.copy2('fileA.txt','fileC.txt')
'fileC.txt'

结果:
linux-ko5m:/home/python/test # stat *
文件:"fileA.txt"
大小:105 块:8 IO 块:4096 普通文件
设备:32h/50d Inode:387 硬链接:1
权限:(0644/-rw-r--r--) Uid:( 0/ root) Gid:( 0/ root)
最近访问:2020-05-18 23:14:00.865010232 +0800
最近更改:2020-05-18 23:13:58.672984945 +0800
最近改动:2020-05-18 23:13:58.672984945 +0800
创建时间:-
文件:"fileC.txt"
大小:105 块:8 IO 块:4096 普通文件
设备:32h/50d Inode:392 硬链接:1
权限:(0644/-rw-r--r--) Uid:( 0/ root) Gid:( 0/ root)
最近访问:2020-05-18 23:14:00.865010232 +0800
最近更改:2020-05-18 23:13:58.672984945 +0800
最近改动:2020-05-18 23:42:13.941638500 +0800
创建时间:-

7、shutil.ignore_patterns(*patterns) --创建一个函数,该函数可用作可调用 copytree() 的忽略参数

8、shutil.copytree(src, dst, symlinks=False, ignore=None) --递归的去拷贝文件夹

>>> shutil.copytree('1/2/', 'a/b/c', symlinks=True, ignore=shutil.ignore_patterns('*.py'))
'a/b/c'

结果:
linux-ko5m:/home/python/test # ll a/b/c/3/
总用量 4
lrwxrwxrwx 1 root root 9 5月 18 23:51 fileAs.txt -> fileA.txt
-rw-r--r-- 1 root root 0 5月 18 23:49 file.txt

9、shutil.rmtree(path[, ignore_errors[, onerror]]) --递归的去删除文件

初始:
linux-ko5m:/home/python/test # ll
总用量 8
drwxr-xr-x 1 root root 2 5月 18 23:48 1
drwxr-xr-x 1 root root 2 5月 19 00:05 a

#执行:
>>> shutil.rmtree('/home/python/test/a')

结果:
linux-ko5m:/home/python/test # ll
总用量 8
drwxr-xr-x 1 root root 2 5月 18 23:48 1

10、shutil.move(src, dst) --递归的去移动文件,它类似mv命令

>>> shutil.move('1','a')
'a'
posted @ 2020-05-20 23:48  静心&得意  阅读(157)  评论(0编辑  收藏  举报