python之模块之shutil模块
shutil
-- --High-level file operations 高级的文件操作模块。
os模块提供了对目录或者文件的新建/删除/查看文件属性,还提供了对文件以及目录的路径操作。比如说:绝对路径,父目录…… 但是,os文件的操作还应该包含移动 复制 打包 压缩 解压等操作,这些os模块都没有提供。
而本章所讲的shutil则就是对os中文件操作的补充。--移动 复制 打包 压缩 解压,
shutil功能:
1 shutil.copyfileobj(fsrc, fdst[, length=16*1024]) #copy文件内容到另一个文件,可以copy指定大小的内容
复制代码
#先来看看其源代码。
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)
if not buf:
break
fdst.write(buf)
#注意! 在其中fsrc,fdst都是文件对象,都需要打开后才能进行复制操作
import shutil
f1=open('name','r')
f2=open('name_copy','w+')
shutil.copyfileobj(f1,f2,length=16*1024)
复制代码
2 shutil.copyfile(src,dst) #copy文件内容,是不是感觉上面的文件复制很麻烦?还需要自己手动用open函数打开文件,在这里就不需要了,事实上,copyfile调用了copyfileobj
查看源代码
shutil.copyfile('name','name_copy_2')
#一句就可以实现复制文件内容
3 shutil.copymode(src,dst) #仅copy权限,不更改文件内容,组和用户。
查看源代码
复制代码
#先看两个文件的权限
[root@slyoyo python_test]# ls -l
total 4
-rw-r--r--. 1 root root 79 May 14 05:17 test1
-rwxr-xr-x. 1 root root 0 May 14 19:10 test2
#运行命令
>>> import shutil
>>> shutil.copymode('test1','test2')
#查看结果
[root@slyoyo python_test]# ls -l
total 4
-rw-r--r--. 1 root root 79 May 14 05:17 test1
-rw-r--r--. 1 root root 0 May 14 19:10 test2
#当我们将目标文件换为一个不存在的文件时报错
>>> shutil.copymode('test1','test3')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/python/lib/python3.4/shutil.py", line 132, in copymode
chmod_func(dst, stat.S_IMODE(st.st_mode))
FileNotFoundError: [Errno 2] No such file or directory: 'test233'
复制代码
4 shutil.copystat(src,dst) #复制所有的状态信息,包括权限,组,用户,时间等
查看源代码
5 shutil.copy(src,dst) #复制文件的内容以及权限,先copyfile后copymode
复制代码
def copy(src, dst, *, follow_symlinks=True):
"""Copy data and mode bits ("cp src dst"). Return the file's destination.
The destination may be a directory.
If follow_symlinks is false, symlinks won't be followed. This
resembles GNU's "cp -P src dst".
If source and destination are the same file, a SameFileError will be
raised.
"""
if os.path.isdir(dst):
dst = os.path.join(dst, os.path.basename(src))
copyfile(src, dst, follow_symlinks=follow_symlinks)
copymode(src, dst, follow_symlinks=follow_symlinks)
return dst
复制代码
6 shutil.copy2(src,dst) #复制文件的内容以及文件的所有状态信息。先copyfile后copystat
查看源代码
7 shutil.copytree(src, dst, symlinks=False, ignore=None, copy_function=copy2,ignore_dangling_symlinks=False) #递归的复制文件内容及状态信息
查看源代码
复制代码
[root@slyoyo python_test]# tree copytree_test/
copytree_test/
└── test
├── test1
├── test2
└── hahaha
[root@slyoyo test]# ls -l
total 0
-rw-r--r--. 1 python python 0 May 14 19:36 hahaha
-rw-r--r--. 1 python python 0 May 14 19:36 test1
-rw-r--r--. 1 root root 0 May 14 19:36 test2
>>> shutil.copytree('copytree_test','copytree_copy')
'copytree_copy'
[root@slyoyo python_test]# ls -l
total 12
drwxr-xr-x. 3 root root 4096 May 14 19:36 copytree_copy
drwxr-xr-x. 3 root root 4096 May 14 19:36 copytree_test
-rw-r--r--. 1 python python 79 May 14 05:17 test1
-rw-r--r--. 1 root root 0 May 14 19:10 test2
[root@slyoyo python_test]# tree copytree_copy/
copytree_copy/
└── test
├── hahaha
├── test1
└── test2
复制代码
8 shutil.rmtree(path, ignore_errors=False, οnerrοr=None) #递归地删除文件
查看源代码
9 shutil.move(src, dst) #递归的移动文件
查看源代码
10 make_archive(base_name, format, root_dir=None, base_dir=None, verbose=0,dry_run=0, owner=None, group=None, logger=None) #压缩打包
查看源代码
base_name: 压缩打包后的文件名或者路径名
format: 压缩或者打包格式 "zip", "tar", "bztar"or "gztar"
root_dir : 将哪个目录或者文件打包(也就是源文件)
复制代码
>>> shutil.make_archive('tarball','gztar',root_dir='copytree_test')
[root@slyoyo python_test]# ls -l
total 12
drwxr-xr-x. 3 root root 4096 May 14 19:36 copytree_copy
drwxr-xr-x. 3 root root 4096 May 14 19:36 copytree_test
-rw-r--r--. 1 root root 0 May 14 21:12 tarball.tar.gz
-rw-r--r--. 1 python python 79 May 14 05:17 test1
-rw-r--r--. 1 root root 0 May 14 19:10 test2
复制代码