python的shutil模块是内置的_python内置模块~shutil

shutil

--High-level file operations,高级的文件操作模块~

shutil 模块的使用主要包括对文件及目录的移动、复制、打包、压缩(解压)、删除等操作。是对os模块的一个补充~

使用示例

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

复制一个文件到另一个文件。

copyfileobj函数源码:

def copyfileobj(fsrc, fdst, length=16*1024):

"""copy data from file-like object fsrc to file-like object fdst"""

while 1:

buf = fsrc.read(length) # length 为缓冲大小

if not buf:

break

fdst.write(buf)

示例:

import shutil

f_read = open(file='/Users/luyi/tmp/a.txt', mode='r')

f_write = open(file='/Users/luyi/tmp/b.txt', mode='w')

shutil.copyfileobj(f_read, f_write)

使用 copyfileobj 进行复制略显麻烦,源文件和目标文件都需要打开(open)才能进行复制操作~

shutil.copyfile(src, dst)

同样是复制操作~

示例:

import shutil

f_src = '/Users/luyi/tmp/a.txt'

f_dest = '/Users/luyi/tmp/b.txt'

shutil.copyfile(f_src, f_dest)

相比于 copyfileobj,copyfile 实现的复制操作要简单的多,在日常的使用中,也是更多的使用 copyfile 函数。其实在内部,copyfile 函数调用了 copyfileobj函数~

shutil.copymode(src, dst)

复制文件的权限,即读写权限,不会复制源文件的内容及属主和属组~

示例:

➜ ls -l

total 16

-rw-rw-r-- 1 baby wheel 4 Nov 7 22:46 a.txt

-rw-r--r-- 1 root wheel 4 Nov 7 22:46 b.txt

>>> import shutil

>>> shutil.copymode('a.txt','b.txt')

➜ ls -l

total 16

-rw-rw-r-- 1 baby wheel 4 Nov 7 22:46 a.txt

-rw-rw-r-- 1 root wheel 4 Nov 7 22:46 b.txt

复制权限的过程,源文件和目标文件都必须存在,若目标文件不存在,则会报错。这里区别于 copyfile 函数,copyfile函数的目标文件可以存在也可以不存在,不存在则直接进行复制,若存在则进行覆盖~

➜ tmp ll

total 8

-rw-r--r-- 1 baby staff 4B Nov 6 23:48 a.txt

>>> import shutil

>>> shutil.copymode('a.txt','b.txt')

Traceback (most recent call last):

File "", line 1, in

File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py", line 91, in copymode

os.chmod(dst, mode)

OSError: [Errno 2] No such file or directory: 'b.txt'

shutil.copystat(src, dst)

复制文件的所有状态信息,包括 mode bits,atime,mtime,flags,但不复制文件内容,以及文件的属主和属组~

➜ ls -l

total 16

-rw-rw-r-- 1 baby wheel 4 Nov 7 22:46 a.txt

-rw-r--r-- 1 root wheel 4 Nov 7 22:50 b.txt

➜ stat a.txt

16777220 8603112152 -rw-rw-r-- 1 baby wheel 0 4 "Nov 7 22:48:16 2018" "Nov 7 22:46:22 2018" "Nov 7 22:46:46 2018" "Nov 7 22:46:06 2018" 4096 8 0 a.txt

➜ stat b.txt

16777220 8603112261 -rw-r--r-- 1 root wheel 0 4 "Nov 7 22:50:32 2018" "Nov 7 22:50:37 2018" "Nov 7 22:50:37 2018" "Nov 7 22:50:32 2018" 4096 8 0 b.txt

>>> import shutil

>>> shutil.copystat('a.txt','b.txt') # 执行复制状态操作

stat 输出的信息中包含3个时间,其中第一个是atime,第二个是mtime,最后一个是ctime。复制完成后,查看两个文件的状态信息:

➜ ls -l

total 16

-rw-rw-r-- 1 baby wheel 4 Nov 7 22:46 a.txt

-rw-rw-r-- 1 root wheel 4 Nov 7 22:46 b.txt

➜ stat a.txt

16777220 8603112152 -rw-rw-r-- 1 baby wheel 0 4 "Nov 7 22:48:16 2018" "Nov 7 22:46:22 2018" "Nov 7 22:46:46 2018" "Nov 7 22:46:06 2018" 4096 8 0 a.txt

➜ stat b.txt

16777220 8603112261 -rw-rw-r-- 1 root wheel 0 4 "Nov 7 22:48:16 2018" "Nov 7 22:46:22 2018" "Nov 7 22:52:02 2018" "Nov 7 22:46:22 2018" 4096 8 0 b.txt

可以看到源文件 a.txt 的 atime,mtime 和 mode bits 已经复制到 b.txt 上,文件的属主和属组 并没有发生变化。其中文件的 ctime 由系统自行维护~

shutil.copy(src, dst)

复制文件的内容以及权限,即先 copyfile,然后再 copymode~

shutil.copy2(src, dst)

复制文件的内容以及文件的状态信息,先 copyfile,再 copystat~

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

递归的复制目录,以及文件的所有状态信息(不包括文件的属主和属组)

示例:

➜ ls -l test1

total 16

-rw-rw-r-- 1 baby wheel 4 Nov 7 22:46 a.txt

-rw-r--r-- 1 root wheel 4 Nov 7 22:56 b.txt

>>> import shutil

>>> shutil.copytree('test1', 'test2')

'test2'

➜ ls -l test2

total 16

-rw-rw-r-- 1 root wheel 4 Nov 7 22:46 a.txt

-rw-r--r-- 1 root wheel 4 Nov 7 22:56 b.txt

shutil.rmtree(path[, ignore_errors[, onerror]])

递归的删除文件~

示例:

➜ ls -l test1

total 16

-rw-rw-r-- 1 baby wheel 4 Nov 7 22:46 a.txt

-rw-r--r-- 1 root wheel 4 Nov 7 22:56 b.txt

>>> import shutil

>>> shutil.rmtree('test1')

rmtree相当于 rm -fr 命令,谨慎操作~

shutil.move(src, dst)

递归的移动文件,作用与通过move命令移动目录类似~

示例:

➜ tmp ll

total 0

drwxr-xr-x 4 baby staff 128B Nov 7 23:09 test

>>> import shutil

>>> shutil.move('test','/Users/luyi')

'/Users/luyi/test'

➜ tmp ll /Users/luyi/test

total 16

-rw-r--r-- 1 baby staff 4B Nov 6 23:48 a.txt

-rw-r--r-- 1 baby staff 4B Nov 7 23:06 b.txt

shutil.make_archive(base_name, format,root_dir=None,...)

打包或压缩文件,并且返回操作后的文件路径~

参数说明:

base_name:压缩包的文件名(abc.tar.gz,则base_name为abc),也可以是绝对路径;若是文件名,则压缩后的文件默认存放于当前目录,若是绝对路径,则压缩有的文件就保存到指定路径~

format:指定压缩或打包的类型,"zip","tar","bztar","gztar"

root_dir:需要压缩的文件或目录路径

owner:属主,默认为当前用户

group:属组,默认为当前组

logger:指定日志对象,通常是logging.Logger对象~

示例:

➜ tmp ll

total 0

drwxr-xr-x 4 baby staff 128B Nov 7 23:09 test

>>> import shutil

>>> shutil.make_archive(base_name='test',format='zip',root_dir='test')

'/Users/luyi/tmp/test.zip'

>>> shutil.make_archive(base_name='test',format='gztar',root_dir='test')

'/Users/luyi/tmp/test.tar.gz'

>>> shutil.make_archive(base_name='test',format='tar',root_dir='test')

'/Users/luyi/tmp/test.tar'

>>> shutil.make_archive(base_name='test',format='bztar',root_dir='test')

'/Users/luyi/tmp/test.tar.bz2'

➜ tmp ll

total 48

drwxr-xr-x 4 baby staff 128B Nov 7 23:09 test

-rw-r--r-- 1 baby staff 10K Nov 7 23:25 test.tar

-rw-r--r-- 1 baby staff 187B Nov 7 23:25 test.tar.bz2

-rw-r--r-- 1 baby staff 195B Nov 7 23:25 test.tar.gz

-rw-r--r-- 1 baby staff 206B Nov 7 23:25 test.zip

.................^_^

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
`shutil` 模块Python 中用于对文件和文件夹进行操作的标准库之一。它提供了一些简单而又强大的函数,使得文件和文件夹的复制、移动、删除等操作变得非常容易。 下面是一些常见的 `shutil` 模块中的函数及其用法: 1. `shutil.copy(src, dst)`:将文件从源路径复制到目标路径。如果目标路径是一个文件,则覆盖该文件;如果目标路径是一个目录,则在该目录下创建同名文件并复制。 ```python import shutil src_path = "source_file.txt" dst_path = "destination_file.txt" shutil.copy(src_path, dst_path) ``` 2. `shutil.copy2(src, dst)`:与 `shutil.copy()` 类似,但还会复制文件的元数据(如创建时间、修改时间等)。 ```python import shutil src_path = "source_file.txt" dst_path = "destination_file.txt" shutil.copy2(src_path, dst_path) ``` 3. `shutil.move(src, dst)`:将文件或目录从源路径移动到目标路径。如果目标路径是一个文件,则覆盖该文件;如果目标路径是一个目录,则在该目录下创建同名文件或目录并移动。 ```python import shutil src_path = "source_file.txt" dst_path = "destination_file.txt" shutil.move(src_path, dst_path) ``` 4. `shutil.rmtree(path)`:递归删除一个目录及其内容。 ```python import shutil dir_path = "directory_to_delete" shutil.rmtree(dir_path) ``` 5. `shutil.make_archive(base_name, format, root_dir)`:创建一个压缩包,并返回压缩包的文件路径。`base_name` 是压缩包的文件名,`format` 是压缩包的格式(如 "zip"、"tar" 等),`root_dir` 是要打包的根目录。 ```python import shutil dir_to_archive = "directory_to_archive" archive_name = "archive" archive_format = "zip" shutil.make_archive(archive_name, archive_format, dir_to_archive) ``` 以上是 `shutil` 模块中常用的一些函数,更多函数可以查看 Python 官方文档。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值