zoukankan      html  css  js  c++  java
  • 常用模块

    一、简介

    shutil – Utility functions for copying and archiving files and directory trees.(用于复制和存档文件和目录树的实用功能。)

    二、实例

    #!/usr/bin/python3
    # -*- coding:utf-8 -*-
    __author__ = 'mayi'
    __date__ = '2018/4/17'
    
    """
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    博客:http://www.cnblogs.com/mayi0312/
    功能:shutil模块的使用
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    """
    
    import shutil
    
    # 1 shutil.copyfileobj(fsrc, fdst[, length=16*1024])
    # copy文件内容到另一个文件,可以copy指定大小的内容
    # 注意! 在其中fsrc,fdst都是文件对象,都需要打开后才能进行复制操作
    f1 = open("1.txt", "r")
    f2 = open("2.txt", "w+")
    shutil.copyfileobj(f1, f2)
    
    # 2 shutil.copyfile(src,dst)
    # copy文件内容,是不是感觉上面的文件复制很麻烦?还需要自己手动用open函数打开
    # 文件,在这里就不需要了,事实上,copyfile调用了copyfileobj
    shutil.copyfile("1.txt", "3.txt")
    
    # 3 shutil.copymode(src,dst)
    # 仅copy权限,不更改文件内容,组和用户。
    shutil.copymode("1.txt", "3.txt")
    
    # 4 shutil.copystat(src,dst)
    # 复制所有的状态信息,包括权限,组,用户,时间等
    shutil.copystat("1.txt", "3.txt")
    
    # 5 shutil.copy(src,dst)
    # 复制文件的内容以及权限,先copyfile后copymode
    shutil.copy("1.txt", "4.txt")
    
    # 6 shutil.copy2(src,dst)
    # 复制文件的内容以及文件的所有状态信息。先copyfile后copystat
    shutil.copy2("1.txt", "5.txt")
    
    # 7 shutil.copytree(src, dst, symlinks=False, ignore=None, copy_function=copy2,ignore_dangling_symlinks=False)
    # 递归的复制文件内容及状态信息
    shutil.copytree("1", "2")
    
    # 8 shutil.rmtree(path, ignore_errors=False, onerror=None)
    # 递归地删除文件
    shutil.rmtree("2")
    
    # 9 shutil.move(src, dst)
    # 递归的移动文件
    shutil.move("1", "2")
    
    # 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("压缩包", "zip", r"2")
    
    # 入口函数
    if __name__ == '__main__':
        pass
    

      

  • 相关阅读:
    match、match_phrase、term示例
    AVL树及java实现
    eclipse集成lombok注解不起作用
    红黑树原理详解
    为什么HashMap中链表长度超过8会转换成红黑树
    用deepin堆砌工作环境
    为什么黑客都不用鼠标?你听说过Linux吗?
    为什么二流程序员都喜欢黑php?
    看Linux 之父是如何定义 Linux?
    Linux 系统故障排查和修复技巧
  • 原文地址:https://www.cnblogs.com/mayi0312/p/8862123.html
Copyright © 2011-2022 走看看