zoukankan      html  css  js  c++  java
  • shutil模块(文件,文件夹,压缩包处理)

    shutil.copyfileobj(fsrc, fdst[, length]) # 将文件内容拷贝到另外一个文件中,可以部分内容

    >>> with open('today.txt') as f:
    ...   print(f.read())
    ...
    明天周一,
    >>> with open('new.txt') as f:
    ...   print(f.read())
    ...
    {"today": "u661fu671fu516d", "mood": "good"}
    
    >>> import shutil
    >>> shutil.copyfileobj(open('today.txt', 'rb'),open('new.txt', 'ab'), 2) # 这里的2应该指的2kb
    >>> with open('new.txt') as f:
    ...   print(f.read())
    ...
    {"today": "u661fu671fu516d", "mood": "good"}明天周一,
    >>> with open('today.txt') as f:
    ...   print(f.read())
    ...
    明天周一,

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

    >>> shutil.copyfile('today.txt', 'copytoday.txt')
    'copytoday.txt'
    >>> with open('copytoday.txt') as f:
    ...   print(f.read())
    ...
    明天周一,

    shutil.copymode(src, dst) # 仅拷贝权限,内容,组,用户都不变

    [xsy@localhost ~]$ ll ad.html
    -rw-rw-r--. 1 xsy xsy 684 6月   6 11:25 ad.html
    [xsy@localhost ~]$ chmod 777 ad.html
    [xsy@localhost ~]$ ll ad.html
    -rwxrwxrwx. 1 xsy xsy 684 6月   6 11:25 ad.html
    [xsy@localhost ~]$ ll triangle.html
    -rw-rw-r--. 1 xsy xsy 432 3月  16 2017 triangle.html
    [xsy@localhost ~]$ python3
    Python 3.4.3 (default, Jun 29 2015, 12:16:01) 
    [GCC 5.1.1 20150618 (Red Hat 5.1.1-4)] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import shutil
    >>> shutil.copymode('ad.html', 'triangle.html')
    >>> 
    KeyboardInterrupt
    >>> 
    [2]+  已停止               python3
    [xsy@localhost ~]$ ll ad.html
    -rwxrwxrwx. 1 xsy xsy 684 6月   6 11:25 ad.html
    [xsy@localhost ~]$ ll triangle.html
    -rwxrwxrwx. 1 xsy xsy 432 3月  16 2017 triangle.html

    shutil.copystat(src, dst) # 拷贝状态的信息,包括:mode bits, atime, mtime, flags

    [xsy@localhost ~]$ stat ad.html
      文件:"ad.html"
      大小:684           块:8          IO 块:4096   普通文件
    设备:fd02h/64770d    Inode:26229689    硬链接:1
    权限:(0777/-rwxrwxrwx)  Uid:( 1000/     xsy)   Gid:( 1000/     xsy)
    环境:unconfined_u:object_r:user_home_t:s0
    最近访问:2018-06-06 13:53:55.883295142 +0800
    最近更改:2018-06-06 11:25:18.142621972 +0800
    最近改动:2018-06-06 13:53:52.747295257 +0800
    创建时间:-
    [xsy@localhost ~]$ stat triangle.html
      文件:"triangle.html"
      大小:432           块:8          IO 块:4096   普通文件
    设备:fd02h/64770d    Inode:26218587    硬链接:1
    权限:(0777/-rwxrwxrwx)  Uid:( 1000/     xsy)   Gid:( 1000/     xsy)
    环境:unconfined_u:object_r:user_home_t:s0
    最近访问:2018-06-06 13:55:08.954292464 +0800
    最近更改:2017-03-16 10:34:03.230491448 +0800
    最近改动:2018-06-06 13:55:05.151292603 +0800
    创建时间:-
    [xsy@localhost ~]$ python3
    Python 3.4.3 (default, Jun 29 2015, 12:16:01) 
    [GCC 5.1.1 20150618 (Red Hat 5.1.1-4)] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import shutil
    >>> shutil.copystat('ad.html', 'triangle.html')>>> 
    [3]+  已停止               python3
    [xsy@localhost ~]$ stat ad.html
      文件:"ad.html"
      大小:684           块:8          IO 块:4096   普通文件
    设备:fd02h/64770d    Inode:26229689    硬链接:1
    权限:(0777/-rwxrwxrwx)  Uid:( 1000/     xsy)   Gid:( 1000/     xsy)
    环境:unconfined_u:object_r:user_home_t:s0
    最近访问:2018-06-06 13:53:55.883295142 +0800
    最近更改:2018-06-06 11:25:18.142621972 +0800
    最近改动:2018-06-06 13:53:52.747295257 +0800
    创建时间:-
    [xsy@localhost ~]$ stat triangle.html
      文件:"triangle.html"
      大小:432           块:8          IO 块:4096   普通文件
    设备:fd02h/64770d    Inode:26218587    硬链接:1
    权限:(0777/-rwxrwxrwx)  Uid:( 1000/     xsy)   Gid:( 1000/     xsy)
    环境:unconfined_u:object_r:user_home_t:s0
    最近访问:2018-06-06 13:59:46.805282281 +0800
    最近更改:2018-06-06 11:25:18.142621972 +0800
    最近改动:2018-06-06 13:59:43.079282417 +0800
    创建时间:-

    shutil.copy(src, dst) # 拷贝文件和权限

    >>> shutil.copy('ad.html', 'ad1.html')
    'ad1.html'
    >>> 
    [4]+  已停止               python3
    [xsy@localhost ~]$ ll ad.html
    -rwxrwxrwx. 1 xsy xsy 684 6月   6 11:25 ad.html
    [xsy@localhost ~]$ ll ad1.html
    -rwxrwxrwx. 1 xsy xsy 684 6月   6 14:02 ad1.html

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

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

    shutil.rmtree(path[, ignore_errors[, oneerror]]) 递归的去删除文件

    shutil.move(src, dst) # 递归的去移动文件

    shutil对压缩包的处理,是调用ZipFile和TarFile两个模块来进行的

    shutil.make_archive(base_name, format, ……) # 创建压缩包并返回文件路径, 例如zip、tar

    base_name 压缩包的路径

    format 压缩包种类:zip tar bztar gztar

    owner 用户,默认当前用户

    group 组,默认当前组

    logger 用于记录日志,通常是logging.Logger对象

  • 相关阅读:
    阿里巴巴集团2014年校园招聘系统project师北京笔试题
    HDU 3104 Combination Lock(数学题)
    MYSQL数据迁移
    nginx配置文件中的location中文详解
    使用JQuery解析、处理JSON数据(应用在课程表)
    OpenResty+lua+GraphicsMagick生成缩略图
    解决使用base64解码太慢的问题,原因是根本就不应该使用此方法解决。
    Bringing up interface eth0: Determining if ip address 10.109.67.81 is already in use for device eth0...
    高效访问Internet-启用ISA Server的缓存
    在OpenResty中使用lua-zlib的方法
  • 原文地址:https://www.cnblogs.com/allenzhang-920/p/9124935.html
Copyright © 2011-2022 走看看