原文:https://www.cnblogs.com/wang-yc/p/5625046.html

一. 简介

  shutil 是高级的文件,文件夹,压缩包处理模块。

二. 使用

shutil.copyfileobj(fsrc, fdst[, length])
将文件内容拷贝到另一个文件中

1
2
3
import shutil
  
shutil.copyfileobj(open('old.xml','r'), open('new.xml''w'))

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

1
shutil.copyfile('f1.log''f2.log')

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

1
shutil.copymode('f1.log''f2.log')

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

1
shutil.copystat('f1.log''f2.log')

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

1
shutil.copy('f1.log''f2.log')

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

1
shutil.copy2('f1.log''f2.log')

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

1
2
3
shutil.copytree('folder1''folder2', ignore=shutil.ignore_patterns('*.pyc''tmp*'))
 
shutil.copytree('f1''f2', symlinks=True, ignore=shutil.ignore_patterns('*.pyc''tmp*'))

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

1
shutil.rmtree('folder1')

shutil.move(src, dst)
递归的去移动文件,它类似mv命令,其实就是重命名。

1
shutil.move('folder1''folder3')

shutil.make_archive(base_name, format,...)

创建压缩包并返回文件路径,例如:zip、tar

创建压缩包并返回文件路径,例如:zip、tar

  • base_name: 压缩包的文件名,也可以是压缩包的路径。只是文件名时,则保存至当前目录,否则保存至指定路径,
    如:www                        =>保存至当前路径
    如:/Users/wupeiqi/www =>保存至/Users/wupeiqi/
  • format: 压缩包种类,“zip”, “tar”, “bztar”,“gztar”
  • root_dir: 要压缩的文件夹路径(默认当前目录)
  • owner: 用户,默认当前用户
  • group: 组,默认当前组
  • logger: 用于记录日志,通常是logging.Logger对象
1
2
3
4
5
6
7
8
#将 /Users/wupeiqi/Downloads/test 下的文件打包放置当前程序目录
import shutil
ret = shutil.make_archive("wwwwwwwwww"'gztar', root_dir='/Users/wupeiqi/Downloads/test')
   
   
#将 /Users/wupeiqi/Downloads/test 下的文件打包放置 /Users/wupeiqi/目录
import shutil
ret = shutil.make_archive("/Users/wupeiqi/wwwwwwwwww"'gztar', root_dir='/Users/wupeiqi/Downloads/test')

  

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

1
2
3
4
5
6
7
8
9
10
11
12
import zipfile
 
# 压缩
= zipfile.ZipFile('laxi.zip''w')
z.write('a.log')
z.write('data.data')
z.close()
 
# 解压
= zipfile.ZipFile('laxi.zip''r')
z.extractall()
z.close()
1
2
3
4
5
6
7
8
9
10
11
12
import tarfile
 
# 压缩
tar = tarfile.open('your.tar','w')
tar.add('/Users/wupeiqi/PycharmProjects/bbs2.log', arcname='bbs2.log')
tar.add('/Users/wupeiqi/PycharmProjects/cmdb.log', arcname='cmdb.log')
tar.close()
 
# 解压
tar = tarfile.open('your.tar','r')
tar.extractall()  # 可设置解压地址
tar.close()

(转)Python3之shutil模块的更多相关文章

  1. Python3之shutil模块

    一. 简介 shutil 是高级的文件,文件夹,压缩包处理模块. 二. 使用 shutil.copyfileobj(fsrc, fdst[, length])将文件内容拷贝到另一个文件中 import ...

  2. Python3 shutil模块

    平时我们总会用到复制文件的命令,Python中自带了相应模块,那就是shutil模块,下面是shutil模块的分析及用法. 1.copyfileobj(fsrc, fdst, length=16*10 ...

  3. python(6)-shutil模块

    高级的 文件.文件夹.压缩包 处理模块 shutil.copyfileobj(fsrc, fdst[, length]) 将文件内容拷贝到另一个文件中: #源码 def copyfileobj(fsr ...

  4. 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时间 ...

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

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

  6. python中的shutil模块

    目录 python中的shutil模块 目录和文件操作 归档操作 python中的shutil模块 shutil模块对文件和文件集合提供了许多高级操作,特别是提供了支持文件复制和删除的函数. 目录和文 ...

  7. Python进阶5---StringIO和BytesIO、路径操作、OS模块、shutil模块

    StringIO StringIO操作 BytesIO BytesIO操作 file-like对象 路径操作 路径操作模块 3.4版本之前:os.path模块 3.4版本开始 建议使用pathlib模 ...

  8. python之模块之shutil模块

    shutil -- --High-level file operations  高级的文件操作模块. os模块提供了对目录或者文件的新建/删除/查看文件属性,还提供了对文件以及目录的路径操作.比如说: ...

  9. shutil模块和os模块对比

    一.shutil -- 是一种高层次的文件操作工具类似于高级API,而且主要强大之处在于其对文件的复制与删除操作更是比较支持好. 1.shutil.copy(src,dst)复制一个文件到另一个目录下 ...

随机推荐

  1. openstack之flavor管理

    概览 [root@cc07 ~]# nova help | grep flavor flavor-access-add Add flavor access for the given tenant. ...

  2. c# 文件笔记

    1.文件属性操作 File类与FileInfo都能实现.静态方法与实例化方法的区别! //use File class Console.WriteLine(File.GetAttributes(fil ...

  3. s5-12 RIP

    什么是RIP? RIP:Routing information protocol,路由选择信息协议 1988年,RFC1058 RIPv1:有类的路由选择协议 RIPv2:无类的路由选择协议,支持CI ...

  4. leaflet入门(一)示例

    代码示例: <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> ...

  5. C#-VS SQLServer数据库编程-摘

    ado.net 通用类对象.在本地内存暂存数据 托管类对象.让本地通用类对象连接数据库,让本地通用类对象和数据库同步 连接数据库 new connection(connectstring) comma ...

  6. 创建私有maven服务器

    私服的创建 1.下载nexus服务  nexus-2.12.0-01-bundle https://pan.baidu.com/s/1o8OfieI 2.下载maven工具   apache-mave ...

  7. (简单广搜) Ice Cave -- codeforces -- 540C

    http://codeforces.com/problemset/problem/540/C You play a computer game. Your character stands on so ...

  8. pycharm的基本使用

    1.设置注释字体的颜色,如下图 2.常用快捷键 Ctrl + Space 基本的代码完成(类.方法.属性) Ctrl + Alt + Space 类名完成 Ctrl + Shift + Enter 语 ...

  9. Android-Kotlin-空值处理&字符串比较&常量

    空值处理: [案例一:] 1.Kotlin对控制处理很严格,默认就不能写null,否则编译都不通过: 描述Car汽车对象: package cn.kotlin.kotlin_base01 /** * ...

  10. Android-Java-解决(多线程存钱案例)的安全隐患-synchronized

    多线程存钱案例: package android.java.thread10; /** * 两个储户,到同一个银行存钱,每个人存了3次,一次1000000.00元 * 1.描述银行 * 2.描述储户任 ...