模組學習步驟一:手冊介紹
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,則將在複製的目錄下產生物理副本來替代符號串連 |