Python shutil模組

來源:互聯網
上載者:User

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

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,則將在複製的目錄下產生物理副本來替代符號串連
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.