zoukankan      html  css  js  c++  java
  • shutil模块

    shutil模块 --高级的文件,文件夹,压缩包处理模块

    常用方法

    1、shutil.copyfileobj(fsrc, fdst[, length]) --将文件内容拷贝到另一个文件中

    >>> import shutil
    >>> shutil.copyfileobj(open('fileA.txt','r'), open('fileB.txt','w'))
    

    2、shutil.copyfile(src, dst) --拷贝文件

    >>> import shutil
    >>> shutil.copyfile('fileA.txt','fileB.txt')
    'fileB.txt'
    

    3、shutil.copymode(src, dst) --仅拷贝权限。内容、组、用户均不变

    初始权限:
    linux-ko5m:/home/python/test # ll
    总用量 8
    -rw-r--r-- 1 root root 105 5月 18 23:13 fileA.txt
    ---------- 1 root root 105 5月 18 23:20 fileB.txt

    执行:
    >>> shutil.copymode('fileA.txt','fileB.txt')
    

    结果:
    linux-ko5m:/home/python/test # ll
    总用量 8
    -rw-r--r-- 1 root root 105 5月 18 23:13 fileA.txt
    -rw-r--r-- 1 root root 105 5月 18 23:20 fileB.txt

    4、shutil.copystat(src, dst) --仅拷贝状态的信息,包括:mode bits, atime, mtime, flag

    初始:
    linux-ko5m:/home/python/test # stat *
    文件:"fileA.txt"
    大小:105 块:8 IO 块:4096 普通文件
    设备:32h/50d Inode:387 硬链接:1
    权限:(0644/-rw-r--r--) Uid:( 0/ root) Gid:( 0/ root)
    最近访问:2020-05-18 23:14:00.865010232 +0800
    最近更改:2020-05-18 23:13:58.672984945 +0800
    最近改动:2020-05-18 23:13:58.672984945 +0800
    创建时间:-
    文件:"fileB.txt"
    大小:105 块:8 IO 块:4096 普通文件
    设备:32h/50d Inode:390 硬链接:1
    权限:(0644/-rw-r--r--) Uid:( 0/ root) Gid:( 0/ root)
    最近访问:2020-05-18 23:20:20.976598184 +0800
    最近更改:2020-05-18 23:20:16.480562975 +0800
    最近改动:2020-05-18 23:23:05.066532066 +0800
    创建时间:-

    执行:
    >>> shutil.copystat('fileA.txt','fileB.txt')
    

    结果:
    linux-ko5m:/home/python/test # stat *
    文件:"fileA.txt"
    大小:105 块:8 IO 块:4096 普通文件
    设备:32h/50d Inode:387 硬链接:1
    权限:(0644/-rw-r--r--) Uid:( 0/ root) Gid:( 0/ root)
    最近访问:2020-05-18 23:14:00.865010232 +0800
    最近更改:2020-05-18 23:13:58.672984945 +0800
    最近改动:2020-05-18 23:13:58.672984945 +0800
    创建时间:-
    文件:"fileB.txt"
    大小:105 块:8 IO 块:4096 普通文件
    设备:32h/50d Inode:390 硬链接:1
    权限:(0644/-rw-r--r--) Uid:( 0/ root) Gid:( 0/ root)
    最近访问:2020-05-18 23:14:00.865010232 +0800
    最近更改:2020-05-18 23:13:58.672984945 +0800
    最近改动:2020-05-18 23:25:25.636249508 +0800
    创建时间:-

    5、shutil.copy(src, dst) --拷贝文件和权限

    >>> shutil.copy('fileA.txt','fileC.txt')
    'fileC.txt'
    

    结果:
    linux-ko5m:/home/python/test # ll
    总用量 8
    -rw-r--r-- 1 root root 105 5月 18 23:13 fileA.txt
    -rw-r--r-- 1 root root 105 5月 18 23:39 fileC.txt

    6、shutil.copy2(src, dst) --拷贝文件和状态信息

    >>> shutil.copy2('fileA.txt','fileC.txt')
    'fileC.txt'
    

    结果:
    linux-ko5m:/home/python/test # stat *
    文件:"fileA.txt"
    大小:105 块:8 IO 块:4096 普通文件
    设备:32h/50d Inode:387 硬链接:1
    权限:(0644/-rw-r--r--) Uid:( 0/ root) Gid:( 0/ root)
    最近访问:2020-05-18 23:14:00.865010232 +0800
    最近更改:2020-05-18 23:13:58.672984945 +0800
    最近改动:2020-05-18 23:13:58.672984945 +0800
    创建时间:-
    文件:"fileC.txt"
    大小:105 块:8 IO 块:4096 普通文件
    设备:32h/50d Inode:392 硬链接:1
    权限:(0644/-rw-r--r--) Uid:( 0/ root) Gid:( 0/ root)
    最近访问:2020-05-18 23:14:00.865010232 +0800
    最近更改:2020-05-18 23:13:58.672984945 +0800
    最近改动:2020-05-18 23:42:13.941638500 +0800
    创建时间:-

    7、shutil.ignore_patterns(*patterns) --创建一个函数,该函数可用作可调用 copytree() 的忽略参数

    8、shutil.copytree(src, dst, symlinks=False, ignore=None) --递归的去拷贝文件夹

    >>> shutil.copytree('1/2/', 'a/b/c', symlinks=True, ignore=shutil.ignore_patterns('*.py'))
    'a/b/c'
    

    结果:
    linux-ko5m:/home/python/test # ll a/b/c/3/
    总用量 4
    lrwxrwxrwx 1 root root 9 5月 18 23:51 fileAs.txt -> fileA.txt
    -rw-r--r-- 1 root root 0 5月 18 23:49 file.txt

    9、shutil.rmtree(path[, ignore_errors[, onerror]]) --递归的去删除文件

    初始:
    linux-ko5m:/home/python/test # ll
    总用量 8
    drwxr-xr-x 1 root root 2 5月 18 23:48 1
    drwxr-xr-x 1 root root 2 5月 19 00:05 a

    #执行:
    >>> shutil.rmtree('/home/python/test/a')
    

    结果:
    linux-ko5m:/home/python/test # ll
    总用量 8
    drwxr-xr-x 1 root root 2 5月 18 23:48 1

    10、shutil.move(src, dst) --递归的去移动文件,它类似mv命令

    >>> shutil.move('1','a')
    'a'
    
  • 相关阅读:
    页面置换算法
    常见内存分配算法
    进程枚举
    NET程序之小试牛刀
    周易起名大师 v18.0算法分析
    VMP分析笔记(cmp命令在VM中的表达)
    一个重启验证软件的算法分析
    一次艰辛的算法分析---------飘零4.0封包分析
    某音频格式转换器算法分析
    一次苦中作乐的追码过程(下)
  • 原文地址:https://www.cnblogs.com/jingxindeyi/p/12927283.html
Copyright © 2011-2022 走看看