13Python标准库系列之shutil模块

简介:

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])

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

1
2
3
4
5
6
7
8
9
>>>  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

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

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

1
2
3
4
5
6
7
8
9
10
>>> 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

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

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

1
2
3
4
5
6
7
8
9
10
>>> 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

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

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

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
>>> 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

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

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

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
>>> 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

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)

递归的去拷贝文件夹

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
>> 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

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

递归的去删除文件

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

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

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

1
2
3
4
5
6
7
8
9
10
>>> os.system( "ls -ld folder1" )
drwxrwxr - 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 - 4  ansheng ansheng  4096  5 月   26  16 : 09  folder3
0

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.

1
2
3
4
5
6
7
8
9
10
>>> os.system( "ls -dl folder3" )
drwxrwxr - 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

可选参数如下:

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

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











本文转自 Edenwy  51CTO博客,原文链接:http://blog.51cto.com/edeny/1925762,如需转载请自行联系原作者
目录
相关文章
|
12天前
|
存储 缓存 JavaScript
python实战篇:利用request库打造自己的翻译接口
python实战篇:利用request库打造自己的翻译接口
26 1
python实战篇:利用request库打造自己的翻译接口
|
17天前
|
存储 开发者 Python
Python中的collections模块与UserDict:用户自定义字典详解
【4月更文挑战第2天】在Python中,`collections.UserDict`是用于创建自定义字典行为的基类,它提供了一个可扩展的接口。通过继承`UserDict`,可以轻松添加或修改字典功能,如在`__init__`和`__setitem__`等方法中插入自定义逻辑。使用`UserDict`有助于保持代码可读性和可维护性,而不是直接继承内置的`dict`。例如,可以创建一个`LoggingDict`类,在设置键值对时记录操作。这样,开发者可以根据具体需求定制字典行为,同时保持对字典内部管理的抽象。
|
18天前
|
存储 缓存 算法
Python中collections模块的deque双端队列:深入解析与应用
在Python的`collections`模块中,`deque`(双端队列)是一个线程安全、快速添加和删除元素的双端队列数据类型。它支持从队列的两端添加和弹出元素,提供了比列表更高的效率,特别是在处理大型数据集时。本文将详细解析`deque`的原理、使用方法以及它在各种场景中的应用。
|
2天前
|
JSON API 数据格式
python的request库如何拿到json的返回值
python的request库如何拿到json的返回值
4 0
|
3天前
|
Python
python学习14-模块与包
python学习14-模块与包
|
5天前
|
SQL 关系型数据库 数据库
Python中SQLite数据库操作详解:利用sqlite3模块
【4月更文挑战第13天】在Python编程中,SQLite数据库是一个轻量级的关系型数据库管理系统,它包含在一个单一的文件内,不需要一个单独的服务器进程或操作系统级别的配置。由于其简单易用和高效性,SQLite经常作为应用程序的本地数据库解决方案。Python的内置sqlite3模块提供了与SQLite数据库交互的接口,使得在Python中操作SQLite数据库变得非常容易。
|
10天前
|
索引 Python
「Python系列」Python operator模块、math模块
Python的`operator`模块提供了一系列内置的操作符函数,这些函数对应于Python语言中的内建操作符。使用`operator`模块可以使代码更加清晰和易读,同时也能提高性能,因为它通常比使用Python内建操作符更快。
27 0
|
15天前
|
数据采集 网络协议 API
python中其他网络相关的模块和库简介
【4月更文挑战第4天】Python网络编程有多个流行模块和库,如requests提供简洁的HTTP客户端API,支持多种HTTP方法和自动处理复杂功能;Scrapy是高效的网络爬虫框架,适用于数据挖掘和自动化测试;aiohttp基于asyncio的异步HTTP库,用于构建高性能Web应用;Twisted是事件驱动的网络引擎,支持多种协议和异步编程;Flask和Django分别是轻量级和全栈Web框架,方便构建不同规模的Web应用。这些工具使网络编程更简单和高效。
|
Unix Python Windows
Python中os和shutil模块实用方法集锦
Python中os和shutil模块实用方法集锦  类型:转载 时间:2014-05-13  这篇文章主要介绍了Python中os和shutil模块实用方法集锦,需要的朋友可以参考下 复制代码代码如下: # os 模块os.sep 可以取代操作系统特定的路径分隔符。
1348 0
|
Unix Linux Python
Python中os和shutil模块实用方法集锦
# os 模块os.sep 可以取代操作系统特定的路径分隔符。windows下为 '\\' os.name 字符串指示你正在使用的平台。比如对于Windows,它是'nt',而对于Linux/Unix用户,它是 'posix' os.getcwd() 函数得到当前工作目录,即当前Python脚本工作的目录路径 os.getenv() 获取一个环境变量,如果没有返回none os.p
1055 0

热门文章

最新文章