将文件内容拷贝到另一个文件

shutil.copyfileobj('fsrc', 'fdst', 'length')

  方法源码:

  1. def copyfileobj(fsrc, fdst, length=16*1024):
  2. # copy data from file-like object fsrc to file-like object fdst
  3. while 1: # 死循环
  4. buf = fsrc.read(length) # 每次读这么长,直到读完
  5. if not buf:
  6. break
  7. fdst.write(buf) # 写入目标文件

  使用:

  1. >>> import shutil
  2. >>> shutil.copyfileobj(open('old.xml','r'), open('new.xml', 'w'))
  3. >>>
  4. >>> f1 = open("sheve_test.py","r")
  5. >>> f2 = open("sheve_test_new.py","w")
  6. >>> shutil.copyfileobj(f1,f2)
文件拷贝

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

  1. shutil.copyfile('f1.log', 'f2.log') #目标文件无需存在

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

  1. >>> import shutil
  2. >>> shutil.copy('test.py', 'test_copy.py')
  3. >>> exit()
  4.  
  5. #:Desktop hqs$ ls -lrt
  6. total 16
  7. -rw-r--r-- 1 hqs staff 124 4 4 10:46 test.py
  8. -rw-r--r-- 1 hqs staff 124 4 4 11:42 test_copy.py

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

  1. >>> import shutil
  2. >>> shutil.copy2('test.py', 'test_copy2.py')

shutil.copymode(src, dsr):拷贝文件权限。内容、组、用户均不变

shutil.copystat(src, dsr):拷贝状态信息。包括:mode\bits\atime\mtime\flags

递归操作

shutil.ignore_patterns(*patterns)

shutil.copytree(src, dst, symlinks=False, ignore=None)  :递归拷贝文件夹,symlinks是软链接,ignore是忽略

shutil.rmtree():递归删除

shutil.move(src, dst):递归移动文件(实质是重命名)

  1. shutil.copytree('packages','pack2')
  2. shutil.copytree('packages','pack3',ignore=shutil.ignore_patterns("__init__.py","view.py"))
  3.  
  4. # shutil.rmtree(path[,ignore_errors[,onerror]]) # 递归地去删除文件
  5. shutil.rmtree("pack2")
  6.  
  7. # shutil.move(src,dst) # 递归地去移动文件(剪切)
  8. shutil.move("pack3","pack4")
文件压缩

shutil.make_archive(base_name, format, ...):创建压缩包并返回文件路径

  1. #将 /data 下的文件打包放置当前程序目录
  2. import shutil
  3. ret = shutil.make_archive("data_bak", 'gztar', root_dir='/data')
  4.  
  5. #将 /data下的文件打包放置 /tmp/目录
  6. import shutil
  7. ret = shutil.make_archive("/tmp/data_bak", 'gztar', root_dir='/data')

  base_name:压缩包文件名或路径(保存到当前目录或指定目录)

  format:压缩包种类(zip\tar\bztar\gztar)

  owner:用户,默认当前用户

  group:组,默认当前组

  logger:用于记录日志,通常是logging.Logger对象

  1. >>> import shutil
  2. >>> shutil.make_archive('test_bak', 'gztar')
  3. 'test_bak.tar.gz'
  4. >>> exit()
  5. # Desktop hqs$ ls -lrt
  6. total 32
  7. -rw-r--r-- 1 hqs staff 124 4 4 10:46 test_copy2.py
  8. -rw-r--r-- 1 hqs staff 124 4 4 10:46 test.py
  9. -rw-r--r-- 1 hqs staff 124 4 4 11:42 test_copy.py
  10. -rw-r--r-- 1 hqs staff 673 4 4 12:07 test_bak.tar.gz
压缩文件处理

zipfile压缩&解压缩

  1. import zipfile
  2.  
  3. # 压缩
  4. z = zipfile.ZipFile('laxi.zip', 'w')
  5. z.write('a.log')
  6. z.write('data.data')
  7. z.close()
  8.  
  9. # 解压
  10. z = zipfile.ZipFile('laxi.zip', 'r')
  11. z.extractall(path='.')
  12. z.close()

tarfile压缩&解压缩

  1. import tarfile
  2.  
  3. # 压缩
  4. >>> t=tarfile.open('/tmp/egon.tar','w')
  5. >>> t.add('/test1/a.py',arcname='a.bak')
  6. >>> t.add('/test1/b.py',arcname='b.bak')
  7. >>> t.close()
  8.  
  9. # 解压
  10. >>> t=tarfile.open('/tmp/egon.tar','r')
  11. >>> t.extractall('/egon')
  12. >>> t.close()

shutil模块——高级的文件、文件夹、压缩包处理模块的更多相关文章

  1. shutil模块(高级的文件、文件夹、压缩包处理模块)

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

  2. shutil 模块 高级的文件、文件夹、压缩包 处理模块

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

  3. shutil——高级的 文件、文件夹、压缩包 处理模块

    高级的 文件.文件夹.压缩包 处理模块 shutil.copyfileobj(fsrc, fdst[, length])复制文件内容(不包含元数据)从类文件对象src到类文件对dst.可选参数leng ...

  4. Python(文件、文件夹压缩处理模块,shelve持久化模块,xml处理模块、ConfigParser文档配置模块、hashlib加密模块,subprocess系统交互模块 log模块)

    OS模块 提供对操作系统进行调用的接口 os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径 os.chdir("dirname")  改变当前脚本工作目 ...

  5. shutil模块和几种文件上传Demo

    一.shutil模块 1.介绍 shutil模块是对os中文件操作的补充.--移动 复制 打包 压缩 解压 2.基本使用 1. shutil.copyfileobj(文件1, 文件2, 长度) 将文件 ...

  6. python文件、文件夹操作OS模块

    转自:python文件.文件夹操作OS模块   '''一.python中对文件.文件夹操作时经常用到的os模块和shutil模块常用方法.1.得到当前工作目录,即当前Python脚本工作的目录路径: ...

  7. Python3-shutil模块-高级文件操作

    Python3中的shutil模块提供了对文件和容器文件的一些高级操作 shutil.copy(src, dst) 拷贝文件,src和dst为路径的字符串表示,copy()会复制文件数据和文件权限,但 ...

  8. nodejs 监听文件夹变化的模块

    使用Node.JS监听文件夹变化 fs.watch 其中Node.JS的文件系统也可侦听某个目录的改变, 如fs.watch   其中fs.watch的最大缺点就是不支持子文件夹的侦听,并且在很多情况 ...

  9. Python之shutil模块(复制移动文件)

    用python实现将某代码文件复制/移动到指定路径下.场景例如:mv ./xxx/git/project1/test.sh ./xxx/tmp/tmp/1/test.sh (相对路径./xxx/tmp ...

  10. 个人永久性免费-Excel催化剂功能第41波-文件文件夹相关函数

    对于日常办公过程中,每天面对的操作离不开文件.文件夹的操作,当然可以用资源管理器.Everything之类的管理软件来管理.但涉及到批量操作时,在Excel环境或许是个更好的方式,前面很多的内容中不断 ...

随机推荐

  1. 老男孩Day11作业:selectors版socket

    一.作业需求: 使用SELECT或SELECTORS模块实现并发简单版FTP 允许多用户并发上传下载文件 二.readme 一.作业需求: 使用SELECT或SELECTORS模块实现并发简单版FTP ...

  2. 二分答案 & 洛谷 P2678 跳石头

    首先让我们先学一下二分答案这个东西...   二分答案,肯定与二分有关,还与可能是答案的东西有关... 二分答案的准确定义: 二分答案是指在答案具有单调性的前提下,利用二分的思想枚举答案,将求解问题转 ...

  3. MAC office2016 安装及激活(试了一下,靠谱, 非常感谢原作者)

    转载地址:https://blog.csdn.net/jxq0816/article/details/77248462 非常感谢原作者. 一.安装包下载地址 http://officecdn.micr ...

  4. Python中的split,rsplit,splitlines

    https://www.cnblogs.com/zhangzengqiang/p/7525175.html

  5. python-继承,父类,子类

    class Spell(object): def __init__(self, incantation, name): self.name = name self.incantation = inca ...

  6. nginx 反向代理导致的session丢失的问题

    [原文链接] https://blog.csdn.net/xiaweiyidengzhewo/article/details/80921750 注意这篇文章解释的是“丢失”而不是“一致性”

  7. Python学习 day03

    一.基本数据类型 python中的基本数据类型有以下几种: int   --  整数     python3中默认整数都是int型,python2中int的范围为-231~232-1(32位系统中)/ ...

  8. 网络编程api bind函数细节 select 细节

    struct sockaddr_in bindaddr; bindaddr.sin_family = AF_INET; bindaddr.sin_addr.s_addr = htonl(INADDR_ ...

  9. zabbix 监控 tomcat

    一, 脚本监控文件 #!/bin/bash # @Function # Find out the highest cpu consumed threads of java, and print the ...

  10. rancher2.X搭建k8s集群平台

    一, 新版特性 Rancher 1.6支持多种容器编排框架,包括Kubernetes.Mesos.Docker Swarm,默认的基础编排引擎是Cattle,Cattle极简的操作体验受到了大量开源社 ...