一、简介

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

  

常用模块 - shutil模块的更多相关文章

  1. os模块,os.path模块,subprocess模块,configparser模块,shutil模块

    1.os模块 os表示操作系统该模块主要用来处理与操作系统相关的操作最常用的文件操作打开 读入 写入 删除 复制 重命名 os.getcwd() 获取当前执行文件所在的文件夹路径os.chdir(&q ...

  2. python day 9: xlm模块,configparser模块,shutil模块,subprocess模块,logging模块,迭代器与生成器,反射

    目录 python day 9 1. xml模块 1.1 初识xml 1.2 遍历xml文档的指定节点 1.3 通过python手工创建xml文档 1.4 创建节点的两种方式 1.5 总结 2. co ...

  3. s14 第5天 时间模块 随机模块 String模块 shutil模块(文件操作) 文件压缩(zipfile和tarfile)shelve模块 XML模块 ConfigParser配置文件操作模块 hashlib散列模块 Subprocess模块(调用shell) logging模块 正则表达式模块 r字符串和转译

    时间模块 time datatime time.clock(2.7) time.process_time(3.3) 测量处理器运算时间,不包括sleep时间 time.altzone 返回与UTC时间 ...

  4. Day5模块-shutil模块

    参考博客:http://www.cnblogs.com/wupeiqi/articles/4963027.html shutil模块是高级的文件.文件夹.压缩处理的模块.比如文件的copy.压缩等. ...

  5. python------模块定义、导入、优化 ------->sys模块,shutil模块

    1.sys模块 import sys sys.argv #命令行参数List,第一个元素是程序本身路径sys.exit(n) #退出程序,正常退出时exit(0).sys.version #获取Pyt ...

  6. python笔记7 logging模块 hashlib模块 异常处理 datetime模块 shutil模块 xml模块(了解)

    logging模块 日志就是记录一些信息,方便查询或者辅助开发 记录文件,显示屏幕 低配日志, 只能写入文件或者屏幕输出 屏幕输出 import logging logging.debug('调试模式 ...

  7. Python全栈之路----常用模块----shutil模块

    高级的 文件.文件包.压缩包 处理模块   参考Python之路[第四篇]:模块     #src是原文件名,fdst是新文件名 shutil.copyfileobj(fsrc, fdst[, len ...

  8. os模块+sys模块+random模块+shutil模块

    os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径os.chdir("dirname") 改变当前脚本工作目录:相当于shell下cdos.curdir ...

  9. day19:os模块&shutil模块&tarfile模块

    os模块:对系统进行操作(6+3) system  popen  listdir  getcwd  chdir  environ / name  sep  linesep import os #### ...

随机推荐

  1. 【Udacity】异常值检测/删除

    Outlier Detection

  2. margin的auto的理解 top,left[,bottom,right] position

    auto auto 总是试图充满父元素 margin有四个值: All the margin properties can have the following values: auto - the ...

  3. 利用Grahics 进行图片裁剪

    这两天做了一个图片对比工具,里面要处理两张大的图片,所以要对图片先进行裁剪最开始用了 /// <summary>        /// 裁剪图片        /// </summa ...

  4. Linux ->> uname命令 -- 查看当前系统信息

    uname命令是用来打印当前Linux操作系统的信息,如果操作系统内核版本,操作系统是32位还是64位,计算机名字,操作系统安装时间,Linux操作系统类型 1) 操作系统是32位还是64位,感觉-m ...

  5. Struts2学习-json

    Struts2 实现JSON输出有2种办法1.把Action变成Servlet,使用传统做法2.使用Struts 内置功能完成 二. 1.导入配置,pom.xml,要去struts2的官网下载mvnr ...

  6. php curl 总结

    1 curl post curl -X POST http://localhost/ -d  '{"data":{"name":"hheh" ...

  7. hiredis

    hiredis是redis开源库对外发布的客户端API包. 当redis-server配置启动后,可以通过hiredis操作redis资源. 主要分为: strings.hash.lists.sets ...

  8. python实现读取类别频数数据画水平条形图

    1.数据分组-->频数分布表 环境配置: import pandas as pd import numpy as np import matplotlib.pyplot as plt 按照你设定 ...

  9. 关于LDA的文章

    转:http://www.zhizhihu.com/html/y2011/3228.html l  Theory n  Introduction u  Unsupervised learning by ...

  10. 迷宫问题——BFS

    改进版 BFS #include <bits/stdc++.h> using namespace std; #define coordi(x,y) ( m*(x-1)+y ) const ...