一、简介

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. eclipse插件开发常见的问题及解决办法

    莫名其妙地我的某个Plug-in Projects出现了这样的Error:An API baseline has not been set for the current workspace.虽然后来 ...

  2. Android 低版本sdk中没有getSupportedPreviewSizes和getSupportedPictureSizes函数怎么办?

    在做camera和SurfaceView做摄像头程序时,需要获取camera支持的相片大小,在低版本sdk中没有getSupportedPictureSizes函数,怎么办呢,请参阅下面的关键代码: ...

  3. CSS 3篇(持续更新)

    1.关于盒子模型 css盒子模型 又称框模型 (Box Model) ,包含了元素内容(content).内边距(padding).边框(border).外边距(margin)几个要素.如图: 理解c ...

  4. 【源码分析】cocostudio场景编辑器的触发器逻辑

    去看场景编辑器的差不多都可以看到有模拟器的设置(菜单栏的设置).默认是选择cocostudio安装路径中的Simulator.exe这个模拟器,看官网介绍是自己可以选择模拟器,而且公开源代码可以按需设 ...

  5. eclipse中对Hadoop项目进行mvn clean install时报错的处理

    [ERROR] Failed to execute goal org.apache.maven.plugins:maven-clean-plugin:2.5:clean (default-clean) ...

  6. 设置dedecms标签 [field:global.autoindex/] 初始值{class递增}

    在{dede:arclist/}这个标签中有个[field:global.autoindex/],是从0开始自增,如果我们想自定义一个数值,比如自定义从2开始.那么就可以写成下面代码: [field: ...

  7. easyui学习笔记12—tab标签页的添加和删除

    这一篇我们来看看标签页的添加和删除动作.我在想看这些例子还不如看文档,文档的内容更加全面,但是文档全部是理论没有实际的操作,看起来很枯燥,文档只能是遇到问题的时候查.easyui的文档写的还是很详细的 ...

  8. SAP S/4HANA里如何创建Customer主数据以及执行后续处理

    来自Jerry的同事Zhang Sean. 1, Launch tcode: BP and select the Organization 2, Maintain the information fo ...

  9. mongoDB 固定集合(capped collection)

    固定集合(Capped Collection)是一种尺寸固定的“循环”集合,可提供高效的创建.读取.删除等操作.这里所指的“循环”的意思是,当分配给集合的文件尺寸耗尽时,就会自动开始删除最初的文档,不 ...

  10. D3——动态绑定数据

    一.绑定数组元素 , , , , ]; d3.select("body") .selectAll("p") .data(dataset) .enter() .a ...