15 Jun 18 复习, shutil模块

15 Jun 18 复习shutil模块(高级的文件、文件夹、压缩包 处理模块)

shutil.copyfileobj(fsrc, fdst[, length])  #将文件内容拷贝到另一个文件中

import shutil

shutil.copyfileobj(open(‘old.xml‘,‘r‘), open(‘new.xml‘, ‘w‘))

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

import shutil

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

shutil.copymode(src, dst)  # 仅拷贝权限。内容、组、用户均不变

import shutil

shutil.copymode(‘f1.log‘, ‘f2.log‘) # 目标文件必须存在

shutil.copystat(src, dst)  # 仅拷贝状态的信息,包括:mode bits, atime, mtime, flags

import shutil

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的意思是排除

拷贝软连接

import shutil

shutil.copytree(‘f1‘, ‘f2‘, symlinks=True, ignore=shutil.ignore_patterns(‘*.pyc‘, ‘tmp*‘))

# 通常的拷贝都把软连接拷贝成硬链接,即对待软连接来说,创建新的文件

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

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‘)

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()

原文地址:https://www.cnblogs.com/zhangyaqian/p/py201806150.html

时间: 2024-08-07 20:38:59

15 Jun 18 复习, shutil模块的相关文章

5 Jun 18 复习,模块

5 Jul 17 复习,内置模块与第三方模块 一.time与datetime import time # 时间戳(timestamp):通常来说,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量. print(time.time())  # 1528188733.8373 # 格式化的时间字符串(Format String) print(time.strftime("%Y-%m-%d %X"))  # 2018-06-05 16:52:13 # 结构化的时间(st

11 Jun 18 复习, pymsql

12   Jun 18 复习 pymysql,orm pymysql 的安装 pip3 install pymysql 2.  pymysql的基本语法 mport pymysql conn = pymysql.connect( host="127.0.0.1", port=3306, user="root", password="123", database="day62", charset="utf8"

13 Jun 18 复习, Mac中不同版本的python调用

13   Jun 18 复习 mac中python的安装 调python3 :/usr/local/bin/python3 调python2(系统自带): /usr/bin/python 在/etc/paths中设置环境变量路径的搜索优先级 当做如上设定时,先检索/usr/local/bin,后检索/usr/bin,故而在cmd中输入python可直接调出python3 原文地址:https://www.cnblogs.com/zhangyaqian/p/py201806130.html

复习shutil模块

shutil.copyfileobj(fsrc, fdst[, length])  #将文件内容拷贝到另一个文件中 import shutil shutil.copyfileobj(open('old.xml','r'), open('new.xml', 'w')) shutil.copyfile(src, dst)  # 拷贝文件 import shutil shutil.copyfile('f1.log', 'f2.log')# 目标文件无需存在 shutil.copymode(src, d

Python3 shutil模块

平时我们总会用到复制文件的命令,Python中自带了相应模块,那就是shutil模块,下面是shutil模块的分析及用法. 1.copyfileobj(fsrc, fdst, length=16*1024) 将源文件拷贝到目标文件,每次拷贝16KB.这个方法中,原文件及目标文件并没有进行关闭,所以这是一个基本方法,并不经常使用. 1 def copyfileobj(fsrc, fdst, length=16*1024): 2 """copy data from file-li

5 Jun 18 jQuery

# 图片太多,详细见link 以及文本 5 Jun 18 一. 今日面试 1. os和sys都是干什么的? os 这个模块提供了一种方便的使用操作系统函数的方法. os: This module provides a portable way of using operating system dependent functionality. sys 这个模块可供访问由解释器使用或维护的变量和与解释器进行交互的函数. sys: This module provides access to som

python 之 random 模块、 shutil 模块、shelve模块、 xml模块

6.12 random 模块 print(random.random()) (0,1)----float 大于0且小于1之间的小数 print(random.randint(1,3)) [1,3] 大于等于1且小于等于3之间的整数 print(random.randrange(1,3)) [1,3) 大于等于1且小于3之间的整数 print(random.choice ( [1,'23', [4,5] ] ) )   1或者23或者[4,5] print(random.sample( [1,'2

shutil模块和os模块对比

一.shutil -- 是一种高层次的文件操作工具类似于高级API,而且主要强大之处在于其对文件的复制与删除操作更是比较支持好. 1.shutil.copy(src,dst)复制一个文件到另一个目录下,返回dst路径.dst可以是一个文件,或者是一个目录.但src必须是一个文件,否则会报错. >>> shutil.copy("e:\\test\\pp.txt","f:\\yy.txt") 'f:\\yy.txt' >>> shut

13Python标准库系列之shutil模块

Python标准库系列之shutil模块 The shutil module offers a number of high-level operations on files and collections of files. In particular, functions are provided which support file copying and removal. For operations on individual files, see also the os modul