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环境或许是个更好的方式,前面很多的内容中不断 ...
随机推荐
- 【spring cloud】并发测试问题
一,问题 并发测试,对外接口测试50个并发的时候开发报错,报错信息类似如下: {"status":"0500","message":&qu ...
- Spring框架的核心模块的作用
Spring框架由7个定义良好的模块(组件)组成,各个模块可以独立存在,也可以联合使用. (1)Spring Core:核心容器提供了Spring的基本功能.核心容器的核心功能是用Ioc容器来管理类的 ...
- PHP 实时生成并下载超大数据量的 Excel 文件
//另外由于excel数据是从数据库里逐步读出然后写入输出流的所以需要将PHP的执行时间设长一点 //(默认30秒)set_time_limit(0)不对PHP执行时间做限制. set_time_li ...
- 关于Manjaro与Ubuntu双系统并存引发的一个boot问题
事情发生在写下这篇博客的半小时前.笔者的电脑本身是Manjaro+win10双系统并存,因为一些原因要安装ubuntu. 装完ubuntu用了一阵子,想切回manjaro,于是遇到了这个问题. 看到k ...
- Docker Run 设置环境变量
Docker Run We can then override the environment variables set in the Docker file when running the im ...
- Luogu P2572 [SCOI2010]序列操作 线段树。。
咕咕了...于是借鉴了小粉兔的做法ORZ... 其实就是维护最大子段和的线段树,但上面又多了一些操作....QWQ 维护8个信息:1/0的个数(sum),左/右边起1/0的最长长度(ls,rs),整段 ...
- 167 Two Sum-Input array is sorted, 125 Valid Palindrome,344
注意这两个元素不能是相同的. 解法一:二分查找法,逐一取数组中的值,然后second = target - numbers[i] , 用二分查找法求第二个值. 时间复杂度:O(nlongn) clas ...
- 1128 N Queens Puzzle (20 分)
The "eight queens puzzle" is the problem of placing eight chess queens on an 8 chessboard ...
- sql语句中变量的写法
$sql = "update cat set num=num+1 where cat_id=$art[cat_id]"; $sql = "update ca ...
- naginx安装入门
一.nginx是什么 nginx是一个开源的,支持高性能,高并发的www服务和代理服务软件.它是一个俄罗斯人lgor sysoev开发的,作者将源代码开源出来供全球使用. nginx比它大哥apach ...