别院牧志知识库 别院牧志知识库
首页
  • 基础

    • 全栈之路
    • 😎Awesome资源
  • 进阶

    • Python 工匠系列
    • 高阶知识点
  • 指南教程

    • Socket 编程
    • 异步编程
    • PEP 系列
  • 面试

    • Python 面试题
    • 2025 面试记录
    • 2022 面试记录
    • 2021 面试记录
    • 2020 面试记录
    • 2019 面试记录
    • 数据库索引原理
  • 基金

    • 基金知识
    • 基金经理
  • 细读经典

    • 德隆-三个知道
    • 孔曼子-摊大饼理论
    • 配置者说-躺赢之路
    • 资水-建立自己的投资体系
    • 反脆弱
  • Git 参考手册
  • 提问的智慧
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
首页
  • 基础

    • 全栈之路
    • 😎Awesome资源
  • 进阶

    • Python 工匠系列
    • 高阶知识点
  • 指南教程

    • Socket 编程
    • 异步编程
    • PEP 系列
  • 面试

    • Python 面试题
    • 2025 面试记录
    • 2022 面试记录
    • 2021 面试记录
    • 2020 面试记录
    • 2019 面试记录
    • 数据库索引原理
  • 基金

    • 基金知识
    • 基金经理
  • 细读经典

    • 德隆-三个知道
    • 孔曼子-摊大饼理论
    • 配置者说-躺赢之路
    • 资水-建立自己的投资体系
    • 反脆弱
  • Git 参考手册
  • 提问的智慧
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • 辨析

  • Sockets编程

  • Django

  • stackoverflow

  • Flask

  • 全栈之路

    • 基础篇

    • 进阶篇

    • 面向对象

    • 网络编程

    • 操作系统

    • 标准库

      • Python 标准库系列之 collections 模块
      • Python 标准库系列之 configparser 模块
      • Python 标准库系列之 datetime 模块
      • Python 标准库系列之 hashlib 模块
      • Python 标准库系列之模块介绍
      • Python 标准库系列之 json 模块
      • Python 标准库系列之 logging 模块
      • Python 标准库系列之 os 模块
      • Python 标准库系列之 random 模块
      • Python 标准库系列之 shutil 模块
        • 文件和目录操作
      • Python 标准库系列之 subprocess 模块
      • Python 标准库系列之 sys 模块
      • Python 标准库系列之 tarfile 模块
      • Python 标准库系列之 time 模块
      • Python 标准库系列之 xml 模块
      • Python 标准库系列之 zipfile 模块
    • 第三方库

    • Python 全栈之路
  • 面试

  • 代码片段

  • 异步编程

  • 😎Awesome资源

  • PEP

  • Python工匠系列

  • 高阶知识点

  • Python 学习资源待整理
  • 设计模式

  • 好“艹蛋”的 Python 呀!
  • FIFO | 待学清单📝
  • pip 安装及使用
  • 数据分析

  • 源码阅读计划

  • OOP

  • 关于 python 中的 setup.py
  • 并行分布式框架 Celery
  • 七种武器,让你的代码提高可维护性
  • 使用 pdb 调试 Python 代码
  • 每周一个 Python 标准库
  • 🐍Python
  • 全栈之路
  • 标准库
佚名
2020-05-23
目录

Python 标准库系列之 shutil 模块

The shutil module offers a number of high-level operations on files and collections of files. In particular, functions are provided which support file copying and removal. For operations on individual files, see also the os module.

对文件、文件夹、压缩包进行处理的模块。

官方文档:https://docs.python.org/3.5/library/shutil.html

# 文件和目录操作

shutil.copyfileobj(fsrc, fdst[, length])

将文件内容拷贝到另一个文件中

>>> import shutil
# 循环读取old.txt文件内容并写入到new.txt文件当中
>>> shutil.copyfileobj(open('old.txt','r'), open('new.txt', 'w'))
>>> import os
# 查看复制过去的文件内容
>>> os.system("cat old.txt new.txt")
old
old
0
1
2
3
4
5
6
7
8
9

*shutil.copyfile(src, dst, , follow_symlinks=True)

拷贝整个文件,没有第二个文件就创建,有就覆盖

>>> os.system("cat old.txt new.txt")
old
new
0
>>> shutil.copyfile('old.txt', 'new.txt')
# 把第二个文件内容给覆盖了
>>> os.system("cat old.txt new.txt")
old
old
0
1
2
3
4
5
6
7
8
9
10

*shutil.copymode(src, dst, , follow_symlinks=True)

仅拷贝文件权限,文件的内容、组、用户均不变

>>> os.system("ls -l old.txt new.txt")
-rw-rw-rw- 1 ansheng ansheng 4 5月  26 15:54 new.txt
-rw-rw-r-- 1 ansheng ansheng 4 5月  26 15:52 old.txt
0
>>> shutil.copymode('old.txt', 'new.txt')
# 文件权限都变成644了
>>> os.system("ls -l old.txt new.txt")
-rw-rw-r-- 1 ansheng ansheng 4 5月  26 15:54 new.txt
-rw-rw-r-- 1 ansheng ansheng 4 5月  26 15:52 old.txt
0
1
2
3
4
5
6
7
8
9
10

*shutil.copystat(src, dst, , follow_symlinks=True)

拷贝文件状态的信息,文件必须存在,不 copy 改动时间

>>> os.system("stat old.txt new.txt")
  文件:'old.txt'
  大小:4               块:8          IO 块:4096   普通文件
设备:801h/2049d        Inode:1835014     硬链接:1
权限:(0664/-rw-rw-r--)  Uid:( 1000/ ansheng)   Gid:( 1000/ ansheng)
最近访问:2016-05-26 15:53:09.813612241 +0800
最近更改:2016-05-26 15:52:54.830640166 +0800
最近改动:2016-05-26 15:52:54.830640166 +0800
创建时间:-
  文件:'new.txt'
  大小:4               块:8          IO 块:4096   普通文件
设备:801h/2049d        Inode:1835024     硬链接:1
权限:(0664/-rw-rw-r--)  Uid:( 1000/ ansheng)   Gid:( 1000/ ansheng)
最近访问:2016-05-26 15:56:22.540041783 +0800
最近更改:2016-05-26 15:54:24.244922722 +0800
最近改动:2016-05-26 15:55:38.353967649 +0800
创建时间:-
0
>>> shutil.copystat('old.txt', 'new.txt')
>>> os.system("stat old.txt new.txt")
  文件:'old.txt'
  大小:4               块:8          IO 块:4096   普通文件
设备:801h/2049d        Inode:1835014     硬链接:1
权限:(0664/-rw-rw-r--)  Uid:( 1000/ ansheng)   Gid:( 1000/ ansheng)
最近访问:2016-05-26 15:53:09.813612241 +0800
最近更改:2016-05-26 15:52:54.830640166 +0800
最近改动:2016-05-26 15:52:54.830640166 +0800
创建时间:-
  文件:'new.txt'
  大小:4               块:8          IO 块:4096   普通文件
设备:801h/2049d        Inode:1835024     硬链接:1
权限:(0664/-rw-rw-r--)  Uid:( 1000/ ansheng)   Gid:( 1000/ ansheng)
最近访问:2016-05-26 15:53:09.813612000 +0800
最近更改:2016-05-26 15:52:54.830640000 +0800
最近改动:2016-05-26 15:56:48.765143115 +0800
创建时间:-
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37

*shutil.copy(src, dst, , follow_symlinks=True)

拷贝文件和状态信息,同样不 copy 改动时间

>>> os.system("stat old.txt new.txt")
  文件:'old.txt'
  大小:4               块:8          IO 块:4096   普通文件
设备:801h/2049d        Inode:1835014     硬链接:1
权限:(0664/-rw-rw-r--)  Uid:( 1000/ ansheng)   Gid:( 1000/ ansheng)
最近访问:2016-05-26 15:53:09.813612241 +0800
最近更改:2016-05-26 15:52:54.830640166 +0800
最近改动:2016-05-26 15:52:54.830640166 +0800
创建时间:-
  文件:'new.txt'
  大小:0               块:0          IO 块:4096   普通空文件
设备:801h/2049d        Inode:1835023     硬链接:1
权限:(0664/-rw-rw-r--)  Uid:( 1000/ ansheng)   Gid:( 1000/ ansheng)
最近访问:2016-05-26 15:57:53.448632439 +0800
最近更改:2016-05-26 15:57:53.448632439 +0800
最近改动:2016-05-26 15:57:53.448632439 +0800
创建时间:-
0
>>> shutil.copy2('old.txt', 'new.txt')
>>> os.system("stat old.txt new.txt")
  文件:'old.txt'
  大小:4               块:8          IO 块:4096   普通文件
设备:801h/2049d        Inode:1835014     硬链接:1
权限:(0664/-rw-rw-r--)  Uid:( 1000/ ansheng)   Gid:( 1000/ ansheng)
最近访问:2016-05-26 15:53:09.813612241 +0800
最近更改:2016-05-26 15:52:54.830640166 +0800
最近改动:2016-05-26 15:52:54.830640166 +0800
创建时间:-
  文件:'new.txt'
  大小:4               块:8          IO 块:4096   普通文件
设备:801h/2049d        Inode:1835023     硬链接:1
权限:(0664/-rw-rw-r--)  Uid:( 1000/ ansheng)   Gid:( 1000/ ansheng)
最近访问:2016-05-26 15:53:09.813612000 +0800
最近更改:2016-05-26 15:52:54.830640000 +0800
最近改动:2016-05-26 15:58:07.938760974 +0800
创建时间:-
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37

*shutil.ignore_patterns(patterns)

This factory function creates a function that can be used as a callable for copytree()‘s ignore argument, ignoring files and directories that match one of the glob-style patterns provided. See the example below.

shutil.copytree(src, dst, symlinks=False, ignore=None, copy_function=copy2, ignore_dangling_symlinks=False)

递归的去拷贝文件夹

>>> os.system("tree folder1")
folder1
├── dir
├── file.txt
├── sc.pyc
├── tmp
└── vgauthsvclog.txt.0 -> /tmp/vgauthsvclog.txt.0

2 directories, 3 files
0
# folder2目录必须不存在,symlinks=True只copy链接文件,如果等于False就copy源文件,ignore等于不copy的文件或者目录
>>> shutil.copytree('folder1', 'folder2', symlinks=False, ignore=shutil.ignore_patterns('*.pyc', 'tmp*'))
>>> os.system("tree folder2")
folder2
├── dir
├── file.txt
└── vgauthsvclog.txt.0

1 directory, 2 files
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

shutil.rmtree(path, ignore_errors=False, onerror=None)

递归的去删除文件

>>> os.system("ls -d folder2")
folder2
0
>>> shutil.rmtree('folder2')
>>> os.system("ls -d folder2")
ls: 无法访问'folder2': 没有那个文件或目录
512
1
2
3
4
5
6
7

shutil.move(src, dst, copy_function=copy2)

递归的去移动文件,它类似 mv 命令,其实就是重命名。

>>> os.system("ls -ld folder1")
drwxrwxr-x 4 ansheng ansheng 4096 5月  26 16:09 folder1
0
>>> shutil.move('folder1', 'folder3')
>>> os.system("ls -ld folder1")
ls: 无法访问'folder1': 没有那个文件或目录
512
>>> os.system("ls -ld folder3")
drwxrwxr-x 4 ansheng ansheng 4096 5月  26 16:09 folder3
0
1
2
3
4
5
6
7
8
9
10

shutil.make_archive(base_name, format[, root_dir[, base_dir[, verbose[, dry_run[, owner[, group[, logger]]]]]]])

Create an archive file (such as zip or tar) and return its name.

>>> os.system("ls -dl folder3")
drwxrwxr-x 4 ansheng ansheng 4096 5月  26 16:21 folder3
0
# /home/ansheng/folder3是保存的文件,gztar是后缀名,/home/ansheng/folder3是要打包的路径
>>> shutil.make_archive("/home/ansheng/folder3", 'gztar', root_dir='/home/ansheng/folder3')
# 返回文件打包放在那儿了
'/home/ansheng/folder3.tar.gz'
>>> os.system("ls -dl /home/ansheng/folder3.tar.gz")
-rw-rw-r-- 1 ansheng ansheng 263 5月  26 16:22 /home/ansheng/folder3.tar.gz
0
1
2
3
4
5
6
7
8
9
10

可选参数如下:

参数 说明
base_name 压缩包的文件名,也可以是压缩包的路径。
format 压缩包种类,“zip”, “tar”, “bztar”,“gztar”
root_dir 要压缩的文件夹路径(默认当前目录)
owner 用户,默认当前用户
group 组,默认当前组

shutil 对压缩包的处理是调用 ZipFile 和 TarFile 两个模块来进行的,后面会介绍这两个模块的使用方法。

编辑 (opens new window)
#编码#面向对象
上次更新: 2024-07-23, 01:00:43
Python 标准库系列之 random 模块
Python 标准库系列之 subprocess 模块

← Python 标准库系列之 random 模块 Python 标准库系列之 subprocess 模块→

最近更新
01
2025 面试记录
05-28
02
提升沟通亲和力的实用策略
03-26
03
工作
07-15
更多文章>
Theme by Vdoing | Copyright © 2019-2025 IMOYAO | 别院牧志
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式