shutil模块的格式

shutil.copyfileobj(文件1,文件2)   #将文件1的数据覆盖copy给文件2

import shutil

f1 = open("1.txt",encoding="utf-8")

f2 = open("2.txt","w",encoding="utf-8")

shutil.copyfileobj(f1,f2)

PS:文件2必须存在,不存在报错

shutil.copyfile(文件1,文件2)   #不用打开文件,直接用文件名进行覆盖copy

import shutil

shutil.copyfile("1.txt","3.txt")

shutil.copymode(文件1,文件2):之拷贝权限,内容组,用户,均不变

def copymode(src,dst):
"""copy mode bits from src to dst"""
if hasattr(os,'chmod'):
st = os.stat(stc)
mode = stat.S_IMODE(st.st_mode)
os.chmod(dst,mode)

shutil.copystat(文件1,文件):只拷贝了权限

def copystat(src,dst):
"""将所有的状态信息(模式位、时间、时间、标志)从src复制到dst"""
st = os.stat(src)
mode = stat.S_IMODE(st.st_mode)
if hasattr(os, 'utime'):
os.utime(dst,(st.st_atime,st.st_mtime))
if hasattr(os, 'chmod')
os.chmod(dst,mode)
if hasattr(os, 'chflags') and hasattr(st,'st_flags'):
try:
os.chflags(dst, st.st_flags)
except OSError,why:
for err in 'EOPNOTSUPP', 'ENOTSUP':
if hasattr(errno,err) and why.errno == getattr(errno, err):
break
else:
raise

shutil.copy(文件1,文件2):拷贝文件和权限都进行copy

def copy(src,dst):
"""copy data and mode bits ("cp src dst")
The destination may be a directory.
"""
if os.path.isdir(dst):
dst = os.path.join(dst,os.path.basename(src))
copyfile(src,dst)
copymode(src,dst)

hutil.make_archive()   #可以压缩,打包文件

import shutil

shutil.make_archive("shutil_archive_test","zip","D:\新建文件夹 (2)")

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

zipfile 压缩解压

import zipfile

# 压缩
z = zipfile.ZipFile('laxi.zip', 'w')
z.write('a.log')
z.write('data.data')
z.close() # 解压
z = zipfile.ZipFile('laxi.zip', 'r')
z.extractall()
z.close()

tarfile 压缩解压

import tarfile

# 压缩
tar = tarfile.open('your.tar','w')
tar.add('/Users/wupeiqi/PycharmProjects/bbs2.zip', arcname='bbs2.zip')
tar.add('/Users/wupeiqi/PycharmProjects/cmdb.zip', arcname='cmdb.zip')
tar.close() # 解压
tar = tarfile.open('your.tar','r')
tar.extractall() # 可设置解压地址
tar.close()

(11)shutil模块(文件处理模块)的更多相关文章

  1. 模块—— 序列化模块、random模块、os模块 、 sys模块、hashlib模块、collections模块

    今天我们来说说Python中的模块: 第三方模块 可以下载/安装/使用 第一步:将pip.exe 所在的目录添加到环境变量中第二步:输入pip第三步:pip install 要安装的模块名称  #pi ...

  2. python3之shutil高级文件操作

    1.shutil高级文件操作模块 shutil模块提供了大量的文件的高级操作.特别针对文件拷贝和删除,主要功能为目录和文件操作以及压缩操作.对单个文件的操作也可参见os模块. 2.shutil模块的拷 ...

  3. python常用标准库(os系统模块、shutil文件操作模块)

    常用的标准库 系统模块 import os 系统模块用于对系统进行操作. 常用方法 os模块的常用方法有数十种之多,本文中只选出最常用的几种,其余的还有权限操作.文件的删除创建等详细资料可以参考官方文 ...

  4. 学习rollup.js模块文件打包

    学习rollup.js模块文件打包 一:rollup 是什么?Rollup 是一个 JavaScript 模块打包器,可以将小块代码编译成大块复杂的代码. webpack 和 Rollup 对比不同点 ...

  5. Python第二十天 shutil 模块 zipfile tarfile 模块

    Python第二十天  shutil 模块  zipfile   tarfile 模块 os文件的操作还应该包含移动 复制  打包 压缩 解压等操作,这些os模块都没有提供 shutil 模块shut ...

  6. os模块、os.path模块、shutil模块、configparser模块、subprocess模块

    一.os模块 os指的是操作系统 该模块主要用于处理与操作系统相关的操作,常用的是文件操作(读.写.删.复制.重命名). os.getcwd()  获取当前文件所在的文件夹路径 os.chdir()  ...

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

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

  8. python 之 random 模块、 shutil 模块、shelve模块、 xml模块

    6.12 random 模块 print(random.random()) (0,1)----float 大于0且小于1之间的小数 print(random.randint(1,3)) [1,3] 大 ...

  9. Python::OS 模块 -- 文件和目录操作

    os模块的简介参看 Python::OS 模块 -- 简介 os模块的进程管理 Python::OS 模块 -- 进程管理 os模块的进程参数 Python::OS 模块 -- 进程参数 os模块中包 ...

随机推荐

  1. Intellij IDEA 搭建Spring Boot项目(一)

    原文出处 第一步选择File –> New –> Project –>Spring Initialer –> 点击Next 第二步自己修改 Group 和 Artifact 字 ...

  2. Thrift0.11.0基于Intellij IDEA的简单的例子

    前言 目前流行的服务调用方式有很多种,例如基于 SOAP 消息格式的 Web Service,基于 JSON 消息格式的 RESTful 服务等.其中所用到的数据传输方式包括 XML,JSON 等,然 ...

  3. jquery添加类

    一.addClass() 方法向被选元素添加一个或多个类. 1.对P元素添加一个intro类.<!DOCTYPE html><html><head lang=" ...

  4. C++面试问题详解

    1.定义一个全局变量放在.cpp文件还是.h文件,原因是什么 在cpp文件中定义变量,h文件用来声明变量的作用域,使用extern声明的变量可以在本编译单元或其他编译单元中使用. 举例如下: a.h文 ...

  5. SSH 远程端口转发

    既然"本地端口转发"是指绑定本地端口的转发,那么"远程端口转发"(remote forwarding)当然是指绑定远程端口的转发. 还是接着看上面那个例子,ho ...

  6. popen strtok 函数的使用

    FILE * popen ( const char * command , const char * type ); int pclose ( FILE * stream );   type 参数只能 ...

  7. POJ 2406 KMP 循环节

    给一个字符串.求这个串的最小的循环节的长度. 好像.num = len/(len-next[len]) 就是循环节的长度.如果 len%(len-next[len]) ==0 就是 说字符串长度刚好是 ...

  8. linux processes identifiers

    Linux, like all Unix uses user and group identifiers to check for access rights to files and images ...

  9. 快速切题 sgu115. Calendar 模拟 难度:0

    115. Calendar time limit per test: 0.25 sec. memory limit per test: 4096 KB First year of new millen ...

  10. codeforce 853A Planning

    题目地址:http://codeforces.com/problemset/problem/853/A 题目大意: 本来安排了 n 架飞机,每架飞机有 ci 的重要度, 第 i 架飞机的起飞时间为 i ...