python之shutilmo模块详解 python之模块之shutil模块

shutil

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

  os模块提供了对目录或者文件的新建/删除/查看文件属性,还提供了对文件以及目录的路径操作。比如说:绝对路径,父目录……  但是,os文件的操作还应该包含移动 复制  打包 压缩 解压等操作,这些os模块都没有提供。

  而本章所讲的shutil则就是对os中文件操作的补充。--移动 复制  打包 压缩 解压,

shutil功能:

1  shutil.copyfileobj(fsrc, fdst[, length=16*1024])    #copy文件内容到另一个文件,可以copy指定大小的内容

  View Code

2  shutil.copyfile(src,dst)   #copy文件内容,是不是感觉上面的文件复制很麻烦?还需要自己手动用open函数打开文件,在这里就不需要了,事实上,copyfile调用了copyfileobj

  查看源代码
shutil.copyfile('name','name_copy_2') #一句就可以实现复制文件内容

3  shutil.copymode(src,dst)   #仅copy权限,不更改文件内容,组和用户。

复制代码
def copymode(src, dst, *, follow_symlinks=True):
    """Copy mode bits from src to dst.

    If follow_symlinks is not set, symlinks aren't followed if and only
    if both `src` and `dst` are symlinks.  If `lchmod` isn't available
    (e.g. Linux) this method does nothing.

    """
    if not follow_symlinks and os.path.islink(src) and os.path.islink(dst): if hasattr(os, 'lchmod'): stat_func, chmod_func = os.lstat, os.lchmod else: return elif hasattr(os, 'chmod'): stat_func, chmod_func = os.stat, os.chmod else: return st = stat_func(src) chmod_func(dst, stat.S_IMODE(st.st_mode))
复制代码
复制代码
#先看两个文件的权限
[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

  查看源代码

6  shutil.copy2(src,dst)    #复制文件的内容以及文件的所有状态信息。先copyfile后copystat

  查看源代码

7  shutil.copytree(src, dst, symlinks=False, ignore=None, copy_function=copy2,ignore_dangling_symlinks=False)   #递归的复制文件内容及状态信息

  查看源代码
  实例

8  shutil.rmtree(path, ignore_errors=False, onerror=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

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

  os模块提供了对目录或者文件的新建/删除/查看文件属性,还提供了对文件以及目录的路径操作。比如说:绝对路径,父目录……  但是,os文件的操作还应该包含移动 复制  打包 压缩 解压等操作,这些os模块都没有提供。

  而本章所讲的shutil则就是对os中文件操作的补充。--移动 复制  打包 压缩 解压,

shutil功能:

1  shutil.copyfileobj(fsrc, fdst[, length=16*1024])    #copy文件内容到另一个文件,可以copy指定大小的内容

  View Code

2  shutil.copyfile(src,dst)   #copy文件内容,是不是感觉上面的文件复制很麻烦?还需要自己手动用open函数打开文件,在这里就不需要了,事实上,copyfile调用了copyfileobj

  查看源代码
shutil.copyfile('name','name_copy_2') #一句就可以实现复制文件内容

3  shutil.copymode(src,dst)   #仅copy权限,不更改文件内容,组和用户。

复制代码
def copymode(src, dst, *, follow_symlinks=True):
    """Copy mode bits from src to dst.

    If follow_symlinks is not set, symlinks aren't followed if and only
    if both `src` and `dst` are symlinks.  If `lchmod` isn't available
    (e.g. Linux) this method does nothing.

    """
    if not follow_symlinks and os.path.islink(src) and os.path.islink(dst): if hasattr(os, 'lchmod'): stat_func, chmod_func = os.lstat, os.lchmod else: return elif hasattr(os, 'chmod'): stat_func, chmod_func = os.stat, os.chmod else: return st = stat_func(src) chmod_func(dst, stat.S_IMODE(st.st_mode))
复制代码
复制代码
#先看两个文件的权限
[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

  查看源代码

6  shutil.copy2(src,dst)    #复制文件的内容以及文件的所有状态信息。先copyfile后copystat

  查看源代码

7  shutil.copytree(src, dst, symlinks=False, ignore=None, copy_function=copy2,ignore_dangling_symlinks=False)   #递归的复制文件内容及状态信息

  查看源代码
  实例

8  shutil.rmtree(path, ignore_errors=False, onerror=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 :         将哪个目录或者文件打包(也就是源文件)

  实例

猜你喜欢

转载自www.cnblogs.com/sui776265233/p/9225253.html