Python shutil模块

简介: 模块学习步骤一:手册介绍 shutil -- High-level file operations 是一种高层次的文件操作工具 类似于高级API,而且主要强大之处在于其对文件的复制与删除操作更是比较支持好。

模块学习步骤一:手册介绍

shutil -- High-level file operations 是一种高层次的文件操作工具

类似于高级API,而且主要强大之处在于其对文件的复制与删除操作更是比较支持好。

相关API介绍

copyfile(src, dst)

 

从源src复制到dst中去。当然前提是目标地址是具备可写权限。抛出的异常信息为

IOException. 如果当前的dst已存在的话就会被覆盖掉。

注意:Special files such as character or block devices and pipes cannot be copied with this function. 不明白这句话的含义了。那硬盘的读写可以不?

copyfileobj(

fsrc, fdst[, length])

Copy the contents of the file-like object fsrc to the file-like object fdst. The integer length, if given, is the buffer size.

copymode(

src, dst)

 

Copy the permission bits from src to dst. The file contents, owner, and group are unaffected. src and dst are path names given as strings.

含义:只是会复制其权限其他的东西是不会被复制的

copystat(src, dst)

Copy the permission bits, last access time, and last modification time from src to dst. The file contents, owner, and group are unaffected. src and dst are path names given as strings.

复制权限、最后访问时间、最后修改时间

copy(src, dst)

Copy the file src to the file or directory dst. If dst is a directory, a file with the same basename as src is created (or overwritten) in the directory specified. Permission bits are copied. src and dst are path names given as strings.

复制一个文件到一个文件或一个目录

copy2(src, dst)

Similar to copy(), but last access time and last modification time are copied as well. This is similar to the Unix command cp -p.

在copy上的基础上再复制文件最后访问时间与修改时间也复制过来了

类似于cp –p的东西

rmtree(path[, ignore_errors[, onerror]])

Delete an entire directory tree (path must point to a directory). If ignore_errors is true, errors resulting from failed removals will be ignored; if false or omitted, such errors are handled by calling a handler specified by onerror or, if that is omitted, they raise an exception.

If onerror is provided, it must be a callable that accepts three parameters: function, path, and excinfo. The first parameter, function, is the function which raised the exception; it will be os.listdir(), os.remove() or os.rmdir(). The second parameter, path, will be the path name passed to function. The third parameter, excinfo, will be the exception information return by sys.exc_info(). Exceptions raised by onerror will not be caught.

move(src, dst)

Recursively move a file or directory to another location.

If the destination is on our current filesystem, then simply use rename. Otherwise, copy src to the dst and then remove src.

说明:如果两个位置的文件系统是一样的话相当于是rename操作,只是改名如果是不在相同的文件系统的话就是做move操作了!

模块学习步骤二:实例

复制一个文件

import os, string, sys, time, re, math, fileinput, glob, shutil

print os.listdir('.')

for file in os.listdir('.'):

if os.path.splitext(file)[1] == ".py":

print file

shutil.copy(file, "a.py")

 

删除一个目录

shutil.rmtree("te")

 

 copyfile( src, dst)  从源src复制到dst中去。当然前提是目标地址是具备可写权限。抛出的异常信息为IOException. 如果当前的dst已存在的话就会被覆盖掉
 copymode( src, dst)  只是会复制其权限其他的东西是不会被复制的
 copystat( src, dst)  复制权限、最后访问时间、最后修改时间
 copy( src, dst)     复制一个文件到一个文件或一个目录
 copy2( src, dst)   在copy上的基础上再复制文件最后访问时间与修改时间也复制过来了,类似于cp –p的东西
 copy2( src, dst)   如果两个位置的文件系统是一样的话相当于是rename操作,只是改名;如果是不在相同的文件系统的话就是做move操作
 copytree(olddir,newdir,True/Flase)  把olddir拷贝一份newdir,如果第3个参数是True,则复制目录时将保持文件夹下的符号连接,如果第3个参数是False,则将在复制的目录下生成物理副本来替代符号连接
目录
相关文章
|
17天前
|
安全 Linux 网络安全
Kali 渗透测试:基于结构化异常处理的渗透-使用Python编写渗透模块(一)
Kali 渗透测试:基于结构化异常处理的渗透-使用Python编写渗透模块(一)
52 2
|
17天前
|
Python Windows 网络安全
Kali 渗透测试:基于结构化异常处理的渗透-使用Python编写渗透模块(二)
Kali 渗透测试:基于结构化异常处理的渗透-使用Python编写渗透模块(二)
43 2
|
17天前
|
Python
Datetime模块应用:Python计算上周周几对应的日期
Datetime模块应用:Python计算上周周几对应的日期
42 1
|
3天前
|
消息中间件 监控 网络协议
Python中的Socket魔法:如何利用socket模块构建强大的网络通信
本文介绍了Python的`socket`模块,讲解了其基本概念、语法和使用方法。通过简单的TCP服务器和客户端示例,展示了如何创建、绑定、监听、接受连接及发送/接收数据。进一步探讨了多用户聊天室的实现,并介绍了非阻塞IO和多路复用技术以提高并发处理能力。最后,讨论了`socket`模块在现代网络编程中的应用及其与其他通信方式的关系。
|
6天前
|
Python
Python 中常用的内置模块之`re`模块
【10月更文挑战第11天】 `re` 模块是 Python 内置的正则表达式处理工具,支持模式匹配、搜索、替换等功能。通过 `search`、`match`、`findall` 和 `sub` 等函数,结合正则表达式的元字符、分组、贪婪模式等特性,可高效完成文本处理任务。示例代码展示了基本用法,帮助快速上手。
9 1
|
6天前
|
JSON 数据格式 Python
Python基础-常用内置模块
【10月更文挑战第11天】 Python 内置模块丰富,涵盖系统交互、时间处理、数学运算、正则表达式、数据序列化等功能,如 `sys`、`os`、`time`、`datetime`、`random`、`math`、`re`、`json`、`pickle` 和 `csv` 等,极大提升了开发效率和代码质量。
8 1
|
10天前
|
Python
Python实用记录(四):os模块-去后缀或者改后缀/指定目录下图片或者子目录图片写入txt/csv
本文介绍了如何使用Python的os模块来操作文件,包括更改文件后缀、分割文件路径和后缀、将指定目录下的所有图片写入txt文档,以及将指定目录下所有子目录中的图片写入csv文档,并为每个子目录分配一个标签。
11 1
|
15天前
|
JSON Java Linux
python有用的模块
python有用的模块
|
17天前
|
安全 测试技术 数据库
Python编程--sys模块及OS模块简单用例
Python编程--sys模块及OS模块简单用例
12 1
|
17天前
|
JSON 数据格式 Python
Python编程:利用JSON模块编程验证用户
Python编程:利用JSON模块编程验证用户
17 1