python常用模块之shutil模块
python常用模块之shutil模块
shutil模块,高级的文件、文件夹、压缩包处理模块
1.shutil.copyfile(src,des[,length]):将文件内容拷贝到另一个文件
In [1]: import os # 导入os模块
In [2]: os.listdir() # 查看当前目录下的文件和目录
Out[2]:
['.XIM-unix',
'.X11-unix',
'.font-unix',
'.Test-unix',
'.ICE-unix',
'replaceContent.py']
In [3]: import shutil # 导入shutil模块
In [4]: shutil.copyfileobj(open('replaceContent.py','r'),open('new_scripts.py','w')) # 以读的形式打开旧文件,再以写的形式写入新文件
In [5]: os.listdir()
Out[5]:
['.XIM-unix',
'.X11-unix',
'.font-unix',
'.Test-unix',
'.ICE-unix',
'replaceContent.py',
'new_scripts.py']
2.shutil.copyfile(src,des):拷贝文件,目标文件无需存在
In [8]: shutil.copyfile("new_scripts.py","scripts.py") # 把旧文件拷贝成为新文件
Out[8]: 'scripts.py'
In [9]: os.listdir()
Out[9]:
['.XIM-unix',
'.X11-unix',
'.font-unix',
'.Test-unix',
'.ICE-unix',
'replaceContent.py',
'new_scripts.py',
'scripts.py'] # 内容和new_scripts.py一样
3.shutil.copymode(src,des):仅拷贝权限。内容、组、用户均不变,目标文件需要存在
# 现在当前目录创建test.py文件,并且将scripts.py权限修改为755
In [11]: shutil.copymode("scripts.py","test.py")
代码运行如下:
[root@host-10-200-137-195 tmp]# ll
total 12
-rw-r--r-- 1 root root 1149 May 9 17:21 new_scripts.py
-rw-r--r-- 1 root root 1149 May 8 09:47 replaceContent.py
-rwxr-xr-x 1 root root 1149 May 9 17:24 scripts.py
-rwxr-xr-x 1 root root 0 May 9 17:26 test.py # 权限相同,但是内容没有复制过来,只复制权限
4.shutil.copystat():仅拷贝文件状态的信息,包括:mode bits atims mtime flags,目标文件必须存在
In [12]: shutil.copystat("scripts.py","test.py")
代码运行如下:
-rwxr-xr-x 1 root root 1149 May 9 17:24 scripts.py
-rwxr-xr-x 1 root root 0 May 9 17:24 test.py # 除了文件内容不一样,其余都是差不多的
5.shutil.copy(src,des):拷贝文件和权限
In [13]: shutil.copy("scripts.py","test.py")
Out[13]: 'test.py'
代码运行如下:
total 16
-rw-r--r-- 1 root root 1149 May 9 17:21 new_scripts.py
-rw-r--r-- 1 root root 1149 May 8 09:47 replaceContent.py
-rwxr-xr-x 1 root root 1149 May 9 17:24 scripts.py
-rwxr-xr-x 1 root root 1149 May 9 17:33 test.py # 只拷贝了文件内容和权限
6.shutil.copy2():拷贝文件和状态信息
# 此时删除test.py文件
In [14]: shutil.copy2("scripts.py","test.py")
Out[14]: 'test.py'
代码运行如下:
[root@host-10-200-137-195 tmp]# ll
total 16
-rw-r--r-- 1 root root 1149 May 9 17:21 new_scripts.py
-rw-r--r-- 1 root root 1149 May 8 09:47 replaceContent.py
-rwxr-xr-x 1 root root 1149 May 9 17:24 scripts.py
-rwxr-xr-x 1 root root 1149 May 9 17:24 test.py
7.shutil.copytree(src,des):递归的去拷贝文件夹,目标目录不能存在
# 首先在/tmp目录下创建a.txt文件
In [19]: shutil.copytree('/tmp/','/a/b/c/',ignore = shutil.ignore_patterns("*.py"))
Out[19]: '/a/b/c/'
代码运行如下:
[root@host-10-200-137-195 c]# pwd
/a/b/c
[root@host-10-200-137-195 c]# ls
a.txt
# 要对/a/b/c目录有写权限
# ignore代表排除
8.shutil.retree(src):递归的删除文件
In [20]: shutil.rmtree('/a/b/c/') # 只需一条命令,就删除了目录
9.shutil.move(src,des):递归的去移动文件,它类似mv命令,其实就是重命名
In [21]: shutil.move('test.py','new_test.py')
Out[21]: 'new_test.py'
In [22]: ll
total 16
-rw-r--r-- 1 root 0 May 9 17:39 a.txt
-rw-r--r-- 1 root 1149 May 9 17:21 new_scripts.py
-rwxr-xr-x 1 root 1149 May 9 17:24 new_test.py*
-rw-r--r-- 1 root 1149 May 8 09:47 replaceContent.py
-rwxr-xr-x 1 root 1149 May 9 17:24 scripts.py*
10.shutil.make_archive(base_name,format,root_dir):创建压缩包并返回文件路径
- base_name:压缩包的文件名,也可以是压缩包的路径。只是文件名时,则保存到当前目录,否则保存刀片指定的目录
- format:压缩包种类,例如:"zip","tar","bztar","gztar"
- root_dir:要压缩的文件夹路径(默认当前目录)
- owner:用户,默认当前用户
- group:组,默认当前组
- logger:用于记录日志,通常时logging.Logger对象
举例看一下:
# 将/tmp下的文件打包放置当前程序目录,压缩包名为yasuo
In [26]: shutil.make_archive("yasuo",'zip','/tmp')
Out[26]: '/tmp/yasuo.zip'
代码运行结果:
[root@host-10-200-137-195 tmp]# ll
total 24
-rw-r--r-- 1 root root 0 May 9 17:39 a.txt
-rw-r--r-- 1 root root 1149 May 9 17:21 new_scripts.py
-rwxr-xr-x 1 root root 1149 May 9 17:24 new_test.py
-rw-r--r-- 1 root root 1149 May 8 09:47 replaceContent.py
-rwxr-xr-x 1 root root 1149 May 9 17:24 scripts.py
-rw-r--r-- 1 root root 4950 May 9 17:59 yasuo.zip
[root@host-10-200-137-195 tmp]# unzip yasuo.zip # 文件已经存在,解压问是否覆盖?
Archive: yasuo.zip
replace replaceContent.py? [y]es, [n]o, [A]ll, [N]one, [r]ename: y
inflating: replaceContent.py
replace new_scripts.py? [y]es, [n]o, [A]ll, [N]one, [r]ename: y
inflating: new_scripts.py
replace scripts.py? [y]es, [n]o, [A]ll, [N]one, [r]ename: y
inflating: scripts.py
replace a.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: y
inflating: a.txt
replace new_test.py? [y]es, [n]o, [A]ll, [N]one, [r]ename: y
inflating: new_test.py
replace yasuo.zip? [y]es, [n]o, [A]ll, [N]one, [r]ename: y
inflating: yasuo.zip
[root@host-10-200-137-195 tmp]# ll
total 20
-rw-r--r-- 1 root root 0 May 9 17:39 a.txt
-rw-r--r-- 1 root root 1149 May 9 17:21 new_scripts.py
-rwxr-xr-x 1 root root 1149 May 9 17:24 new_test.py
-rw-r--r-- 1 root root 1149 May 8 09:47 replaceContent.py
-rwxr-xr-x 1 root root 1149 May 9 17:24 scripts.py
-rw-r--r-- 1 root root 3319 May 9 17:59 yasuo.zip
我们也可以将/tmp目录下的文件打包放在/opt目录下重新命名:
In [29]: shutil.make_archive("/opt/aaa",'gztar','/tmp')
Out[29]: '/opt/aaa.tar.gz'
运行结果:
[root@host-10-200-137-195 opt]# ll
total 4
-rw-r--r-- 1 root root 2120 May 9 18:04 aaa.tar.gz
[root@host-10-200-137-195 opt]# ll -a
total 8
drwxr-xr-x. 2 root root 23 May 9 18:04 .
dr-xr-xr-x. 19 root root 4096 May 9 18:03 ..
-rw-r--r-- 1 root root 2120 May 9 18:04 aaa.tar.gz
[root@host-10-200-137-195 opt]# tar -zxvf aaa.tar.gz
./
./.XIM-unix/
./.X11-unix/
./.font-unix/
./.Test-unix/
./.ICE-unix/
./replaceContent.py
./new_scripts.py
./scripts.py
./a.txt
./new_test.py
./yasuo.zip
shutil对压缩包的处理是通过ZipFile和TarFile两个模块进行的,详细:
zipfile 压缩&解压缩
压缩:
In [1]: import zipfile # 导入这个模块
In [2]: z = zipfile.ZipFile("压缩.zip",'w')
In [3]: z.write('a.txt') # 写入的内容应该是文件名
In [4]: z.write('new_test.py')
In [5]: z.close() # 关闭
此时运行完毕后,会在/tmp 的目录下生成"压缩.zip"文件
解压缩:先删除a.txt和new_test.py文件
In [10]: z = zipfile.ZipFile('压缩.zip','r')
In [12]: z.extractall('.') # 表示在当前目录下完成解压
In [13]: z.close()
那么在关闭的时候,文件就已经都回来了。不信你看:
-rw-r--r-- 1 root root 0 May 9 18:14 a.txt
-rw-r--r-- 1 root root 1149 May 9 17:21 new_scripts.py
-rw-r--r-- 1 root root 1149 May 9 18:14 new_test.py
-rw-r--r-- 1 root root 1149 May 8 09:47 replaceContent.py
-rwxr-xr-x 1 root root 1149 May 9 17:24 scripts.py
-rw-r--r-- 1 root root 1355 May 9 18:10 压缩.zip
tarfile压缩和解压缩
压缩:
In [1]: import tarfile
In [2]: t = tarfile.open("/opt/yasuo.tar",'w')
In [3]: t.add('a.txt',arcname="a.txt.bak") # 把a.txt.bak文件放到yasuo.tar进行压缩
In [4]: t.close()
代码运行完,就在/opt的目录下创建了yasuo.tar文件了
解压缩:
In [6]: t = tarfile.open('/opt/yasuo.tar','r')
In [7]: t.extractall('/tmp') # 将它解压到/tmp目录下
In [8]: t.close()
此时,a.txt.tar已经回到/tmp目录下了。
python常用模块之shutil模块的更多相关文章
- [转]python中对文件、文件夹的操作——os模块和shutil模块常用说明
转至:http://l90z11.blog.163.com/blog/static/187389042201312153318389/ python中对文件.文件夹的操作需要涉及到os模块和shuti ...
- Python中os和shutil模块实用方法集…
Python中os和shutil模块实用方法集锦 类型:转载 时间:2014-05-13 这篇文章主要介绍了Python中os和shutil模块实用方法集锦,需要的朋友可以参考下 复制代码代码如下: ...
- Python中os和shutil模块实用方法集锦
Python中os和shutil模块实用方法集锦 类型:转载 时间:2014-05-13 这篇文章主要介绍了Python中os和shutil模块实用方法集锦,需要的朋友可以参考下 复制代码代码如下: ...
- Python常用内置模块之xml模块
xml即可扩展标记语言,它可以用来标记数据.定义数据类型,是一种允许用户对自己的标记语言进行定义的源语言.从结构上,很像HTML超文本标记语言.但他们被设计的目的是不同的,超文本标记语言被设计用来显示 ...
- os模块和shutil模块
# coding=utf-8 import os path="D:\\test" ######### 目录结构如下 # test # / \ \ \ # test01 test02 ...
- 元数据的概念以及相关的操作os模块、shutil模块
查看文件的元数据 stat [OPTION]… FILE… OPTION: -f 输出文件系统的状态,而非文件的状态 -t 显示简要格式的文件元数据信息 FILE:可同时查看多个文件的元数据信息,多个 ...
- Python进阶5---StringIO和BytesIO、路径操作、OS模块、shutil模块
StringIO StringIO操作 BytesIO BytesIO操作 file-like对象 路径操作 路径操作模块 3.4版本之前:os.path模块 3.4版本开始 建议使用pathlib模 ...
- python之模块之shutil模块
shutil -- --High-level file operations 高级的文件操作模块. os模块提供了对目录或者文件的新建/删除/查看文件属性,还提供了对文件以及目录的路径操作.比如说: ...
- Python入门-模块2(sys模块、shutil 模块)
sys模块: sys.argv 命令行参数List,第一个元素是程序本身路径 sys.exit(n) 退出程序,正常退出时exit(0) sys.version 获取Python解释程序的版本信息 s ...
随机推荐
- Centos 7 关闭邮件服务及禁用IPv6
关闭邮件服务(禁用25端口) sudo systemctl stop dovecot sudo systemctl stop postfix sudo systemctl disable doveco ...
- vector vector int 初始化
方法一: vector<vector<int>>array=(2,vector<int>()); array[0].push_back(1); array[i].p ...
- nginx配置Strict Transport Security
一个网站接受一个HTTP的请求,然后跳转到HTTPS,用户可能在开始跳转前,通过没有加密的方式和服务器对话,比如,用户输入http://zt.test.com或者直接zt.test.com.这样存在中 ...
- Windows Server 2008驱动安装全攻略
安装设备驱动程序原本是一件非常简单的事情,很多驱动程序在安装的时候我们只要不停单击“下一步”按钮,就能让驱动程序顺利地在对应计算机系统“落户”;不过,当身边的计算机系统升级为Windows Serve ...
- zip unzip tar 压缩解压
yum install -y unzip zip yum安装zip -r mydata.zip mydata mydata目录压缩为mydata.zipunzip mydata.zip - ...
- MongoDB 副本集和C#交互,简单测试
MongoDB 副本集和C#交互,简单测试 primary节点宕机: 模拟primary节点宕机的情况,这时查看监控: 可以看到37018已经成了primary节点.主界面宕机导致了整个集群发生一次e ...
- [spring mvc]Hello World入门
1.新建项目 File->New->Other,选择Dynamic web project: 项目建好之后,目录结构如下: 2.WEB-INF/web.xml 中配置 dispatcher ...
- ZooKeeper原理 --------这可能是把ZooKeeper概念讲的最清楚的一篇文章
相信大家对 ZooKeeper 应该不算陌生,但是你真的了解 ZooKeeper 是什么吗?如果别人/面试官让你讲讲 ZooKeeper 是什么,你能回答到哪个地步呢? 我本人曾经使用过 ZooKee ...
- JSP 表达式语言
JSP 表达式语言 JSP表达式语言(EL)使得访问存储在JavaBean中的数据变得非常简单.JSP EL既可以用来创建算术表达式也可以用来创建逻辑表达式.在JSP EL表达式内可以使用整型数,浮点 ...
- flask学习(八):页面跳转和重定向
1. 用处:在用户访问一些需要登录的页面的时候,如果用户没有登录,那么让页面重定向到登录页面 2. 实例 运行效果: 用户已登录,进入发布问答页面 用户未登录,跳转到登录页面