转载自:http://www.cnblogs.com/xiaowuyi/archive/2012/03/08/2385808.html

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

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,  given,  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操作了!

模块学习步骤二:实例

复制一个文件

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

 os.listdir()

 file  os.listdir():

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

 file

shutil.copy(file, )

 

删除一个目录

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,则将在复制的目录下生成物理副本来替代符号连接