Python shutil模塊


模塊學習步驟一:手冊介紹

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,則將在復制的目錄下生成物理副本來替代符號連接


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM