python模块之shutil和zipfile
shutil 模块
高级的 文件、文件夹、压缩包 处理模块
shutil.copyfileobj(fsrc, fdst[, length])
将文件内容拷贝到另一个文件中
import shutil
shutil.copyfileobj(open('old.xml','r'), open('new.xml', 'w'))
shutil.copyfile(src, dst)
拷贝文件
shutil.copyfile('f1.log', 'f2.log') #目标文件无需存在
shutil.copymode(src, dst)
仅拷贝权限。内容、组、用户均不变
shutil.copymode('f1.log', 'f2.log') #目标文件必须存在
shutil.copystat(src, dst)
仅拷贝状态的信息,包括:mode bits, atime, mtime, flags
shutil.copystat('f1.log', 'f2.log') #目标文件必须存在
shutil.copy(src, dst)
拷贝文件和权限
import shutil
shutil.copy('f1.log', 'f2.log')
shutil.copy2(src, dst)
拷贝文件和状态信息
import shutil
shutil.copy2('f1.log', 'f2.log')
shutil.ignore_patterns(*patterns)
shutil.copytree(src, dst, symlinks=False, ignore=None)
递归的去拷贝文件夹
import shutil
shutil.copytree('folder1', 'folder2', ignore=shutil.ignore_patterns('*.pyc', 'tmp*')) #目标目录不能存在,注意对folder2目录父级目录要有可写权限,ignore的意思是排除
shutil.rmtree(path[, ignore_errors[, onerror]])
递归的去删除文件
import shutil
shutil.rmtree('folder1')
shutil.move(src, dst)
递归的去移动文件,它类似mv命令,其实就是重命名。
import shutil
shutil.move('folder1', 'folder3')
shutil.make_archive(base_name, format,...)
创建压缩包并返回文件路径,例如:zip、tar
创建压缩包并返回文件路径,例如:zip、tar
- base_name: 压缩包的文件名,也可以是压缩包的路径。只是文件名时,则保存至当前目录,否则保存至指定路径,
如 data_bak =>保存至当前路径
如:/tmp/data_bak =>保存至/tmp/
- format: 压缩包种类,“zip”, “tar”, “bztar”,“gztar”
- root_dir: 要压缩的文件夹路径(默认当前目录)
- owner: 用户,默认当前用户
- group: 组,默认当前组
- logger: 用于记录日志,通常是logging.Logger对象
#将 /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')
代码示例:
import shutil
with open('shutil_test.py', 'r', encoding='utf-8') as f1:
with open('shutil_test_new.py', 'w', encoding='utf-8') as f2:
shutil.copyfileobj(f1, f2) # 新建了f2文件,并且f1的内容拷贝到f2了
# copyfile,不用读文件内容了,直接copy
shutil.copyfile('shutil_test.py', 'shutil_test_new.py')
# copymode,只把权限copy过去,目标文件必须存在
shutil.copymode('shutil_test.py', 'shutil_test_new.py')
shutil.copytree('my_proj', 'my_proj_copy', ignore=shutil.ignore_patterns('__init__.py', 'views.py'))
# copy除__init__.py', 'views.py的所有内容,也支持*.py,就是copy除py文件的所有内容
shutil.rmtree('my_proj_copy', ignore_errors=True) # 递归删除如果有权限错误就忽略
shutil.move('my_proj_copy', 'm3') # 其实就等于重命名了
# 压缩
shutil.make_archive('tmp/m3', 'zip', 'm3', owner='root') # 压缩文件到tmp目录下
zipfile和tarfile
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(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()
代码示例:
import zipfile
# 压缩
# z = zipfile.ZipFile('test.zip', 'w') # 要压缩的文件名
# z.write('m3')
# z.write('1.time.py')
# z.write('my_proj')
# z.close()
# 解压
z = zipfile.ZipFile('../test.zip', 'r') # 在终端执行,解压到当前目录
z.extractall()
z.close()
import tarfile
t = tarfile.open('test2.tar', 'w')
t.add('/Users/liangshaohua/PycharmProjects/lufei_basci_practice/第二模块/第2章.常用模块/my_proj', arcname='proj2')
t.add('../2.datetime.py')
t.close()
python模块之shutil和zipfile的更多相关文章
- Python中模块之shutil及zipfile&tarfile的功能介绍
shutil的功能介绍及其他打包.压缩模块 1. shutil模块的方法 chown 更改指定路径的属组 2. copy 拷贝文件和权限 方法:shutil.copy(src,dst,*,follow ...
- python笔记07-----打包模块(shutil,zipfile,tarfile)
1.shutil模块 复制删除 import shutil shutil.copy('filename', 'test2') # copy方法 f1 = open('filename',encodin ...
- python模块之shutil
shutil是一个用于简化文件操作的模块. 复制文件(传入源文件对象和目标文件对象) import shutil f1 = open(r'/Users/jingxing/PycharmProjects ...
- python模块:shutil
"""Utility functions for copying and archiving files and directory trees. XXX The fun ...
- python模块之shutil高级文件操作
简介 shutil模块提供了大量的文件的高级操作.特别针对文件拷贝和删除,主要功能为目录和文件操作以及压缩操作.对单个文件的操作也可参见os模块. 注意即便是更高级别的文件复制函数(shutil.co ...
- Python模块:shutil、序列化(json&pickle&shelve)、xml
shutil模块: 高级的 文件.文件夹.压缩包 处理模块 shutil.copyfileobj(fscr,fdst [, length]) # 将文件内容拷贝到另一个文件中 import shu ...
- Python 模块之shutil模块
#拷贝文件,可指定长度,fsrc和fdst都是一个文件对象 def copyfileobj(fsrc, fdst, length=16*1024) shutil.copyfileobj(open(&q ...
- Python之文件与目录操作及压缩模块(os、shutil、zipfile、tarfile)
Python中可以用于对文件和目录进行操作的内置模块包括: 模块/函数名称 功能描述 open()函数 文件读取或写入 os.path模块 文件路径操作 os模块 文件和目录简单操作 zipfile模 ...
- Python第二十天 shutil 模块 zipfile tarfile 模块
Python第二十天 shutil 模块 zipfile tarfile 模块 os文件的操作还应该包含移动 复制 打包 压缩 解压等操作,这些os模块都没有提供 shutil 模块shut ...
随机推荐
- 不可见类有抽象父类,spring配置子类bean,注入父类,aop就可以切父类的方法
public class TestBeanChild { int b = 1; public TestBean createDefault() { return new TestBeanDefault ...
- sesstionStorage和localStorage
使用: 对于多页面的pc端,为了同步多页面的消息提醒,可以将数据储存在localStorage中,多页面共享同一个localStorage.然后使用setInterval轮询获取数据,执行逻辑代码 s ...
- github 新建一个分支
我能说今天在github上新建分支的时候懵逼了半天吗..为了下次不再懵逼,还是在这里记录一下吧.. 进入你的项目---code---Branch----点击那个倒三角-----你会发现一个输入框(这是 ...
- P4874 回形遍历 —模拟
思路: 写完后信心满满,结果超时. 我很不解,下了个数据结果——,z竟然是大于1e10的,跟题目给的不一样啊 原来如此,正解是一行一行的走的... 注意当到两边一样近时,应优先向下和右!!!!!! 这 ...
- SpringMVC的基础配置及视图定位
概要 记录一下搭建SpringMVC框架的步骤 视图定位也就是改变jsp在项目中的路径 一.新建javaweb项目springmvc1,在lib中导入jar包 此项目上传了GitHub,方便去下载ja ...
- IO(File、递归)
第1章 File 1.1 IO概述 回想之前写过的程序,数据都是在内存中,一旦程序运行结束,这些数据都没有了,等下次再想使用这些数据,可是已经没有了.那怎么办呢?能不能把运算完的数据都保存下来,下 ...
- mongodb集合的增删
1.创建集合 createCollection() 方法 MongoDB db.createCollection(name, options) 是用来创建集合. 语法: 基本的 createColle ...
- html学习笔记-XML-Javascript
html学习笔记-XML-Javascript Table of Contents 1. XML HTTP Request 1.1. XMLHttpRequest 对象 1.2. 创建 XMLHttp ...
- js根据鼠标方向划入遮罩层
js根据鼠标方向划入遮罩层: <!DOCTYPE html> <html lang="en"> <head> <meta charset= ...
- JavaScript笔记2
1.浮点数在运算过程中会产生误差,因为计算机无法精确表示无限循环小数. 要比较两个浮点数是否相等,只能计算它们之差的绝对值,看是否小于某个阈值(某个可接受的范围):Math.abs(1 / 3 - ( ...