Python中shutil模組的學習筆記教程,pythonshutil

來源:互聯網
上載者:User

Python中shutil模組的學習筆記教程,pythonshutil

介紹

shutil 名字來源於 shell utilities,有學習或瞭解過Linux的人應該都對 shell 不陌生,可以藉此來記憶模組的名稱。該模組擁有許多檔案(夾)操作的功能,包括複製、移動、重新命名、刪除等等

一、chutil.copy(source, destination)

shutil.copy() 函數實現檔案複製功能,將 source 檔案複製到 destination 檔案夾中,兩個參數都是字串格式。如果 destination 是一個檔案名稱,那麼它會被用來當作複製後的檔案名稱,即等於 複製 + 重新命名。

舉例如下:

>> import shutil >> import os >> os.chdir('C:\') >> shutil.copy('C:\spam.txt', 'C:\delicious') 'C:\delicious\spam.txt' >> shutil.copy('eggs.txt', 'C:\delicious\eggs2.txt') 'C:\delicious\eggs2.txt'

如代碼所示,該函數的傳回值是複製成功後的字串格式的檔案路徑

二、shutil.copytree(source, destination)

shutil.copytree()函數複製整個檔案夾,將 source 檔案夾中的所有內容複寫到 destination 中,包括 source 裡面的檔案、子檔案夾都會被複製過去。兩個參數都是字串格式。

注意:如果 destination 檔案夾已經存在,該操作並返回一個 FileExistsError 錯誤,提示檔案已存在。即表示,如果執行了該函數,程式會自動建立一個新檔案夾(destination參數)並將 source 檔案夾中的內容複寫過去

舉例如下:

>> import shutil >> import os >> os.chdir('C:\') >> shutil.copytree('C:\bacon', 'C:\bacon_backup') \'C:\bacon_backup'

如以上代碼所示,該函數的傳回值是複製成功後的檔案夾的絕對路徑字串

所以該函數可以當成是一個備份功能

三、shutil.move(source, destination)

shutil.move() 函數會將 source 檔案或檔案夾移動到 destination 中。傳回值是移動後檔案的絕對路徑字串。

如果 destination 指向一個檔案夾,那麼 source 檔案將被移動到 destination 中,並且保持其原有名字。例如:

>> import shutil >> shutil.move('C:\bacon.txt', 'C:\eggs') 'C:\eggs\bacon.txt'

上例中,如果 C:\eggs 檔案夾中已經存在了同名檔案 bacon.txt,那麼該檔案將被來自於 source 中的同名檔案所重寫。

如果 destination 指向一個檔案,那麼 source 檔案將被移動並重新命名,如下:

>> shutil.move('C:\bacon.txt', 'C:\eggs\new_bacon.txt') 'C:\eggs\new_bacon.txt'

等於是移動+重新命名

<b>注意,如果 destination 是一個檔案夾,即沒有帶尾碼的路徑名,那麼 source 將被移動並重新命名為 destination</b>,如下:

>> shutil.move('C:\bacon.txt', 'C:\eggs') 'C:\eggs'

即 bacon.txt 檔案已經被重新命名為 eggs,是一個沒有檔案尾碼的檔案

最後,destination 檔案夾必須是已經存在的,否則會引發異常:

>> shutil.move('spam.txt', 'C:\does_not_exist\eggs\ham') Traceback (most recent call last): File "D:\Python36\lib\shutil.py", line 538, in move os.rename(src, real_dst) FileNotFoundError: [WinError 3] 系統找不到指定的路徑。: 'test.txt' -> 'C:\does_not_exist\eggs\ham' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "

四、永久性刪除檔案和檔案夾

這裡有涉及到 os 模組中的相關函數

os.unlink(path) 會刪除 path 路徑檔案

os.rmdir(path) 會刪除 path 路徑檔案夾,但是這個檔案夾必須是空的,不包含任何檔案或子檔案夾

shutil.rmtree(path) 會刪除 path 路徑檔案夾,並且在這個檔案夾裡面的所有檔案和子檔案夾都會被刪除

利用函數執行刪除操作時,應該倍加謹慎,因為如果想要刪除 txt 檔案,而不小心寫到了 rxt ,那麼將會給自己帶來麻煩

此時,我們可以利用字串的 endswith 屬性對檔案格式進行檢查與篩選

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作能帶來一定的協助,如果有疑問大家可以留言交流,謝謝大家對幫客之家的支援。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.