shutil模块——高级的文件、文件夹、压缩包处理模块
将文件内容拷贝到另一个文件 |
shutil.copyfileobj('fsrc', 'fdst', 'length')
方法源码:
- def copyfileobj(fsrc, fdst, length=16*1024):
- # copy data from file-like object fsrc to file-like object fdst
- while 1: # 死循环
- buf = fsrc.read(length) # 每次读这么长,直到读完
- if not buf:
- break
- fdst.write(buf) # 写入目标文件
使用:
- >>> import shutil
- >>> shutil.copyfileobj(open('old.xml','r'), open('new.xml', 'w'))
- >>>
- >>> f1 = open("sheve_test.py","r")
- >>> f2 = open("sheve_test_new.py","w")
- >>> shutil.copyfileobj(f1,f2)
文件拷贝 |
shutil.copyfile(src, dst):拷贝文件
- shutil.copyfile('f1.log', 'f2.log') #目标文件无需存在
shutil.copy(src, dsr):拷贝文件和权限
- >>> import shutil
- >>> shutil.copy('test.py', 'test_copy.py')
- >>> exit()
- #:Desktop hqs$ ls -lrt
- total 16
- -rw-r--r-- 1 hqs staff 124 4 4 10:46 test.py
- -rw-r--r-- 1 hqs staff 124 4 4 11:42 test_copy.py
shutil.copy2(src, dsr):拷贝文件和状态信息
- >>> import shutil
- >>> 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):递归移动文件(实质是重命名)
- shutil.copytree('packages','pack2')
- shutil.copytree('packages','pack3',ignore=shutil.ignore_patterns("__init__.py","view.py"))
- # shutil.rmtree(path[,ignore_errors[,onerror]]) # 递归地去删除文件
- shutil.rmtree("pack2")
- # shutil.move(src,dst) # 递归地去移动文件(剪切)
- shutil.move("pack3","pack4")
文件压缩 |
shutil.make_archive(base_name, format, ...):创建压缩包并返回文件路径
- #将 /data 下的文件打包放置当前程序目录
- import shutil
- ret = shutil.make_archive("data_bak", 'gztar', root_dir='/data')
- #将 /data下的文件打包放置 /tmp/目录
- import shutil
- ret = shutil.make_archive("/tmp/data_bak", 'gztar', root_dir='/data')
base_name:压缩包文件名或路径(保存到当前目录或指定目录)
format:压缩包种类(zip\tar\bztar\gztar)
owner:用户,默认当前用户
group:组,默认当前组
logger:用于记录日志,通常是logging.Logger对象
- >>> import shutil
- >>> shutil.make_archive('test_bak', 'gztar')
- 'test_bak.tar.gz'
- >>> exit()
- # Desktop hqs$ ls -lrt
- total 32
- -rw-r--r-- 1 hqs staff 124 4 4 10:46 test_copy2.py
- -rw-r--r-- 1 hqs staff 124 4 4 10:46 test.py
- -rw-r--r-- 1 hqs staff 124 4 4 11:42 test_copy.py
- -rw-r--r-- 1 hqs staff 673 4 4 12:07 test_bak.tar.gz
压缩文件处理 |
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(path='.')
- z.close()
tarfile压缩&解压缩
- import tarfile
- # 压缩
- >>> t=tarfile.open('/tmp/egon.tar','w')
- >>> t.add('/test1/a.py',arcname='a.bak')
- >>> t.add('/test1/b.py',arcname='b.bak')
- >>> t.close()
- # 解压
- >>> t=tarfile.open('/tmp/egon.tar','r')
- >>> t.extractall('/egon')
- >>> t.close()
shutil模块——高级的文件、文件夹、压缩包处理模块的更多相关文章
- shutil模块(高级的文件、文件夹、压缩包处理模块)
shutil 模块 高级的 文件.文件夹.压缩包 处理模块 shutil.copyfileobj(fsrc, fdst[, length])将文件内容拷贝到另一个文件中 import shutil s ...
- shutil 模块 高级的文件、文件夹、压缩包 处理模块
高级的文件.文件夹.压缩包 处理模块 # 将文件内容拷贝到另一个文件中 shutil.copyfileobj(fsrc, fdst[, length]) import shutil shutil.co ...
- shutil——高级的 文件、文件夹、压缩包 处理模块
高级的 文件.文件夹.压缩包 处理模块 shutil.copyfileobj(fsrc, fdst[, length])复制文件内容(不包含元数据)从类文件对象src到类文件对dst.可选参数leng ...
- Python(文件、文件夹压缩处理模块,shelve持久化模块,xml处理模块、ConfigParser文档配置模块、hashlib加密模块,subprocess系统交互模块 log模块)
OS模块 提供对操作系统进行调用的接口 os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径 os.chdir("dirname") 改变当前脚本工作目 ...
- shutil模块和几种文件上传Demo
一.shutil模块 1.介绍 shutil模块是对os中文件操作的补充.--移动 复制 打包 压缩 解压 2.基本使用 1. shutil.copyfileobj(文件1, 文件2, 长度) 将文件 ...
- python文件、文件夹操作OS模块
转自:python文件.文件夹操作OS模块 '''一.python中对文件.文件夹操作时经常用到的os模块和shutil模块常用方法.1.得到当前工作目录,即当前Python脚本工作的目录路径: ...
- Python3-shutil模块-高级文件操作
Python3中的shutil模块提供了对文件和容器文件的一些高级操作 shutil.copy(src, dst) 拷贝文件,src和dst为路径的字符串表示,copy()会复制文件数据和文件权限,但 ...
- nodejs 监听文件夹变化的模块
使用Node.JS监听文件夹变化 fs.watch 其中Node.JS的文件系统也可侦听某个目录的改变, 如fs.watch 其中fs.watch的最大缺点就是不支持子文件夹的侦听,并且在很多情况 ...
- Python之shutil模块(复制移动文件)
用python实现将某代码文件复制/移动到指定路径下.场景例如:mv ./xxx/git/project1/test.sh ./xxx/tmp/tmp/1/test.sh (相对路径./xxx/tmp ...
- 个人永久性免费-Excel催化剂功能第41波-文件文件夹相关函数
对于日常办公过程中,每天面对的操作离不开文件.文件夹的操作,当然可以用资源管理器.Everything之类的管理软件来管理.但涉及到批量操作时,在Excel环境或许是个更好的方式,前面很多的内容中不断 ...
随机推荐
- 老男孩Day11作业:selectors版socket
一.作业需求: 使用SELECT或SELECTORS模块实现并发简单版FTP 允许多用户并发上传下载文件 二.readme 一.作业需求: 使用SELECT或SELECTORS模块实现并发简单版FTP ...
- 二分答案 & 洛谷 P2678 跳石头
首先让我们先学一下二分答案这个东西... 二分答案,肯定与二分有关,还与可能是答案的东西有关... 二分答案的准确定义: 二分答案是指在答案具有单调性的前提下,利用二分的思想枚举答案,将求解问题转 ...
- MAC office2016 安装及激活(试了一下,靠谱, 非常感谢原作者)
转载地址:https://blog.csdn.net/jxq0816/article/details/77248462 非常感谢原作者. 一.安装包下载地址 http://officecdn.micr ...
- Python中的split,rsplit,splitlines
https://www.cnblogs.com/zhangzengqiang/p/7525175.html
- python-继承,父类,子类
class Spell(object): def __init__(self, incantation, name): self.name = name self.incantation = inca ...
- nginx 反向代理导致的session丢失的问题
[原文链接] https://blog.csdn.net/xiaweiyidengzhewo/article/details/80921750 注意这篇文章解释的是“丢失”而不是“一致性”
- Python学习 day03
一.基本数据类型 python中的基本数据类型有以下几种: int -- 整数 python3中默认整数都是int型,python2中int的范围为-231~232-1(32位系统中)/ ...
- 网络编程api bind函数细节 select 细节
struct sockaddr_in bindaddr; bindaddr.sin_family = AF_INET; bindaddr.sin_addr.s_addr = htonl(INADDR_ ...
- zabbix 监控 tomcat
一, 脚本监控文件 #!/bin/bash # @Function # Find out the highest cpu consumed threads of java, and print the ...
- rancher2.X搭建k8s集群平台
一, 新版特性 Rancher 1.6支持多种容器编排框架,包括Kubernetes.Mesos.Docker Swarm,默认的基础编排引擎是Cattle,Cattle极简的操作体验受到了大量开源社 ...