模塊學(xué)習(xí)步驟一:手冊介紹
shutil -- High-level file operations 是一種高層次的文件操作工具
類似于高級API详拙,而且主要強大之處在于其對文件的復(fù)制與刪除操作更是比較支持好佃迄。
相關(guān)API介紹
copyfile(src, dst)
從源src復(fù)制到dst中去款违。當然前提是目標地址是具備可寫權(quán)限徘意。拋出的異常信息為
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.
含義:只是會復(fù)制其權(quán)限其他的東西是不會被復(fù)制的
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.
復(fù)制權(quán)限、最后訪問時間逞力、最后修改時間
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.
復(fù)制一個文件到一個文件或一個目錄
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上的基礎(chǔ)上再復(fù)制文件最后訪問時間與修改時間也復(fù)制過來了
類似于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.
說明:如果兩個位置的文件系統(tǒng)是一樣的話相當于是rename操作曙寡,只是改名如果是不在相同的文件系統(tǒng)的話就是做move操作了!
模塊學(xué)習(xí)步驟二:實例
復(fù)制一個文件
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復(fù)制到dst中去寇荧。當然前提是目標地址是具備可寫權(quán)限举庶。拋出的異常信息為IOException. 如果當前的dst已存在的話就會被覆蓋掉
copymode( src, dst) 只是會復(fù)制其權(quán)限其他的東西是不會被復(fù)制的
copystat( src, dst) 復(fù)制權(quán)限、最后訪問時間揩抡、最后修改時間
copy( src, dst) 復(fù)制一個文件到一個文件或一個目錄
copy2( src, dst) 在copy上的基礎(chǔ)上再復(fù)制文件最后訪問時間與修改時間也復(fù)制過來了户侥,類似于cp –p的東西
copy2( src, dst) 如果兩個位置的文件系統(tǒng)是一樣的話相當于是rename操作,只是改名峦嗤;如果是不在相同的文件系統(tǒng)的話就是做move操作
copytree(olddir,newdir,True/Flase) 把olddir拷貝一份newdir蕊唐,如果第3個參數(shù)是True,則復(fù)制目錄時將保持文件夾下的符號連接烁设,如果第3個參數(shù)是False替梨,則將在復(fù)制的目錄下生成物理副本來替代符號連接