您的位置:首页 > 编程语言 > Python开发

Python shutil模块

2013-01-06 13:37 316 查看
模块学习步骤一:手册介绍

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

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

相关API介绍

?
 

从源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的东西

说明:如果两个位置的文件系统是一样的话相当于是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,则将在复制的目录下生成物理副本来替代符号连接
http://www.cnblogs.com/xiaowuyi/archive/2012/03/08/2385808.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: