shutil,zipfile,tarfile模块
一,shutil模块
1、shutil.chown()
shutil.chown('test.txt',user='mysql',group='mysql') #改变文件的属主和属组.
2、shutil.copy()
shutil.copy('test.txt','test_copy.txt') #拷贝文件
3、shutil.copy2()
shutil.copy2('test.txt','test_copy2.txt') #拷贝文件并复制所有统计信息,如修改时间等。
4、shutil.copyfile()
shutil.copyfile('test_ln.txt','test_copyfile.txt') #如果是链接文件,将复制新文件,不复制链接
5、shutil.copyfielobj()
shutil.copyfileobj(srcf,dstf,length=2) #按长度拷贝文件对象
6、shutil.copymode()
shutil.copyfileobj(srcf,dstf,length=2) #按长度拷贝文件对象
7、shutil.copystat()
shutil.copystat('test.txt','test_copymode.txt') #拷贝文件的访问和修改时间,其他不受影响
8、shutil.copytree()
shutil.copytree(src, dst, symlinks=False, ignore=None) #递归的去拷贝文件夹
9、shutil.rmtree()
shutil.rmtree(path[, ignore_errors[, onerror]]) #递归的去删除文件
10、shutil.move()
shutil.move(src, dst) #递归的去移动文件,它类似mv命令,其实就是重命名。
11、shutil.make_archive()
make_archive(base_name, format, root_dir=None, base_dir=None, verbose=0,
dry_run=0, owner=None, group=None, logger=None)
'''创建压缩包并返回文件路径:
base_name:压缩包的文件名,也可以是压缩包的路径,只是文件名时,保存到当前目录,否则保存到指定路径
format:压缩包种类,‘zip’,'tar','bztar','gztar'
root_dir:要压缩的文件夹路径(默认当前目录)
owner:用户,默认当前用户
group:组,默认当前组
logger:用于记录日志,通常是logging.Logger对象
'''
#将/root目录下的所有文件压缩到media目录下取名为www,压缩格式为tar
ret = shutil.make_archive("/media/www",'tar',root_dir='/root')
#将文件以tar格式压缩到当前目录下
ret = shutil.make_archive("ipython55",'tar',root_dir='/root/ipython-5.5.0')
二,模块zipfile,tarfile。
import zipfile
#压缩
>>> z = zipfile.ZipFile('xin.tar.gz','w') #创建名为xin.tar.gz的压缩文件
>>> z.write('test.txt') #写入文件到压缩文件中
>>> z.write('log.txt')
>>> z.close() #关闭文件
#解压缩
>>> z = zipfile.ZipFile('xin.tar.gz','r') #打开压缩文件
>>> z.extractall(path='/python/day7') #解压到指定路径下
>>> z.close()
>>> import tarfile
#压缩
>>> tar = tarfile.open('/usr/targzfile.tar.gz','w') #指定目录创建压缩文件
>>> tar.add('/python/day7/test1.py',arcname='test1.py') #添加文件到压缩文件中
>>> tar.add('/python/day7/test1.py',arcname='test2.py')
>>> tar.close()
#解压缩
>>> tar = tarfile.open('/usr/targzfile.tar.gz','w') #指定目录创建压缩文件
>>> tar.add('/python/day7/test1.py',arcname='test1.py') #添加文件到压缩文件中
>>> tar.add('/python/day7/test1.py',arcname='test2.py')
>>> tar.close()
微信扫码,介绍更全面详细。

shutil,zipfile,tarfile模块的更多相关文章
- Python3学习之路~5.6 shutil & zipfile & tarfile模块
高级的 文件.文件夹.压缩包 处理模块 shutil.copyfileobj(fsrc, fdst[, length])#将文件内容拷贝到另一个文件中,可以部分内容 shutil.copyfile(s ...
- Python第二十天 shutil 模块 zipfile tarfile 模块
Python第二十天 shutil 模块 zipfile tarfile 模块 os文件的操作还应该包含移动 复制 打包 压缩 解压等操作,这些os模块都没有提供 shutil 模块shut ...
- (常用)time,datetime,random,shutil(zipfile,tarfile),sys模块
a.time模块import time 时间分为三种形式1.时间戳 (时间秒数的表达形式, 从1970年开始)print(time.time())start_time=time.time()time. ...
- 模块 - random/string/os/sys/shutil/zipfile/tarfile
random 模块 方法: >>> random.randint(1,3) #会包含 1 2 3 3 >>> random.randrange(1,3) #会包含 ...
- time/datetime/random/string/os/sys/shutil/zipfile/tarfile - 总结
time 模块: time.time() #时间戳 time.localtime() #当前时间对象元组 time.localtime(123123) #根据时间戳的时间对象 time.mktime( ...
- zipfile tarfile模块
zipfile --- 使用ZIP存档 这个模块提供了创建.读取.写入.添加及列出 ZIP 文件的工具 # 创建一个ZipFile对象, 可使用上下文管理 with class zipfile.Zip ...
- Python中模块之shutil及zipfile&tarfile的功能介绍
shutil的功能介绍及其他打包.压缩模块 1. shutil模块的方法 chown 更改指定路径的属组 2. copy 拷贝文件和权限 方法:shutil.copy(src,dst,*,follow ...
- day17.json模块、时间模块、zipfile模块、tarfile模块
一.json模块 """ 所有的编程语言都能够识别的数据格式叫做json,是字符串 能够通过json序列化成字符串与如下类型: (int float bool str l ...
- 【Python】 压缩文件处理 zipfile & tarfile
[zipfile] 虽然叫zipfile,但是除了zip之外,rar,war,jar这些压缩(或者打包)文件格式也都可以处理. zipfile模块常用的一些操作和方法: is_zipfile(file ...
- Python的压缩文件处理 zipfile & tarfile
本文从以下两个方面, 阐述Python的压缩文件处理方式: 一. zipfile 二. tarfile 一. zipfile 虽然叫zipfile,但是除了zip之外,rar,war,jar这些压缩( ...
随机推荐
- Codeforces Round #528-A. Right-Left Cipher(字符串模拟)
time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...
- GIT主要用到的命令
git add . //添加到暂存盘 git commit -m ‘备注’//提交到本地仓库 git push //提交到远程仓库 fetch更新本地仓库两种方式: //方法一 $ git fetch ...
- IIS服务器访问网站出现403错误的解决方法
最近用织梦做了一个网站,因为织梦会在一个文件夹中生成网站的静态页面,而我们单击某个栏目时,一般程序都是直接去寻找该文件夹中的index.html文件的,当服务器中默认的索引文件不包括index.htm ...
- P3290 寻找第K大数
描述 寻找第K大数 N个小朋友在一起做游戏.每个小朋友在自己的硬纸板上写一个数,然后同时举起来.接着,小y老师提一个问题,看哪个小朋友先抢答出来.问题是:在这N个数中,第K大的是哪个数?请你编程完成. ...
- 死磕 java并发包之LongAdder源码分析
问题 (1)java8中为什么要新增LongAdder? (2)LongAdder的实现方式? (3)LongAdder与AtomicLong的对比? 简介 LongAdder是java8中新增的原子 ...
- 基本类型包装类、System类、Math类、Arrays类、大数据运算
1 基本类型包装类 Java中想对8种基本数据类型进行复杂操作很困难. 实际程序界面上用户输入的数据都是以字符串类型进行存储的. 程序开发中,需要把字符串转换成指定的基本数据类型. 1.1基本数据类型 ...
- cf519D. A and B and Interesting Substrings(前缀和)
题意 给出$26$个字母对应的权值和一个字符串 问满足以下条件的子串有多少 首尾字母相同 中间字母权值相加为0 Sol 我们要找到区间满足$sum[i] - sum[j] = 0$ $sum[i] = ...
- Xiaocms 去版权
Xiaocms 去版权 后台去版权: 1. 登录页面 修改文件:\admin\template\login.tpl.php 代码: <td width="190" rows ...
- css:hover伪类的使用
:hover的使用,即当鼠标指针移入元素时,所做出的样式设置 示例一 <!DOCTYPE html> <html lang="en"> <head&g ...
- 阿里开发搜索(OpenSearch)的学习与使用
概述: 开放搜索(OpenSearch)是一款结构化数据搜索托管服务,为移动应用开发者和网站站长提供简单.高效.稳定.低成本和可扩展的搜索解决方案. OpenSearch基于阿里巴巴自主研发的大规模分 ...