Python 的 Shutil 模塊,你都了解多少?
shutil 是 Python 標準庫中的一個模塊,提供了許多用于文件操作和目錄操作的功能。無論是需要復制、移動、重命名、刪除文件,還是進行目錄操作,shutil 都是一個強大的工具。
本文將會學習到 shutil 模塊,包括其主要功能和示例代碼,以幫助你更好地理解如何使用它來處理文件和目錄。
1、什么是shutil模塊?
shutil 模塊是 Python 標準庫中的一個核心模塊,提供了用于文件和目錄操作的功能,包括復制、移動、重命名、刪除文件和目錄等。
shutil 模塊是基于高級文件操作庫 os 模塊構建的,提供了更高級別的文件操作接口,使文件和目錄的處理更加方便。
通過 shutil 模塊,可以執行以下操作:
- 復制文件和目錄。
- 移動文件和目錄。
- 重命名文件和目錄。
- 刪除文件和目錄。
- 創建和刪除目錄。
- 執行文件操作的遞歸操作。
- 更多與文件和目錄操作相關的功能。
shutil 模塊是 Python 編程中的一個不可或缺的工具,可以更輕松地管理文件和目錄。
2、文件和目錄操作
(1)復制文件
shutil.copy(src, dst) 函數用于復制文件。將源文件復制到目標位置。
示例代碼:
import shutil
# 復制文件
shutil.copy("source_file.txt", "destination_file.txt")
(2)復制目錄
shutil.copytree(src, dst) 函數用于復制目錄及其內容。遞歸地復制整個目錄結構。
示例代碼:
import shutil
# 復制目錄
shutil.copytree("source_directory", "destination_directory")
(3)移動文件或目錄
shutil.move(src, dst) 函數用于移動文件或目錄。可以用于重命名文件或將文件或目錄從一個位置移動到另一個位置。
示例代碼:
import shutil
# 移動文件
shutil.move("source_file.txt", "destination_file.txt")
# 移動目錄
shutil.move("source_directory", "destination_directory")
(4)重命名文件或目錄
shutil.move(src, dst) 函數也可以用于重命名文件或目錄。通過將新路徑傳遞給 dst 參數,可以實現重命名操作。
示例代碼:
import shutil
# 重命名文件
shutil.move("old_file.txt", "new_file.txt")
# 重命名目錄
shutil.move("old_directory", "new_directory")
(5)刪除文件
shutil.remove(file) 函數用于刪除文件。
示例代碼:
import shutil
# 刪除文件
shutil.remove("file_to_delete.txt")
(6)刪除目錄
shutil.rmtree(directory) 函數用于遞歸地刪除目錄及其內容。
示例代碼:
import shutil
# 刪除目錄及其內容
shutil.rmtree("directory_to_delete")
(7)創建目錄
shutil.mkdir(directory) 函數用于創建目錄。
示例代碼:
import shutil
# 創建目錄
shutil.mkdir("new_directory")
(8)刪除目錄中的文件
shutil.rmtree(directory) 函數會刪除目錄及其內容。如果只想刪除目錄中的文件但保留目錄結構,可以使用以下方法:
import shutil
# 刪除目錄中的文件,保留目錄結構
for root, dirs, files in os.walk("directory_to_clean"):
for file in files:
file_path = os.path.join(root, file)
os.remove(file_path)
3、文件操作的遞歸操作
shutil 模塊提供了許多遞歸操作的函數,可以在文件操作中非常有用。這些函數可以遞歸地處理文件和目錄,從而簡化復雜的操作。
(1)遞歸復制
在 Python 中,使用 shutil.copytree(src, dst) 函數可以遞歸復制整個目錄結構,包括子目錄和文件。這個函數非常有用,可以將一個目錄及其所有內容復制到另一個位置,保留了整個目錄結構。
使用 shutil.copytree 來遞歸復制目錄:
import shutil
# 源目錄和目標目錄
source_directory = "source_directory"
destination_directory = "destination_directory"
# 使用 copytree 復制源目錄到目標目錄
shutil.copytree(source_directory, destination_directory)
print(f"Directory '{source_directory}' has been recursively copied to '{destination_directory}'.")
在上面的示例中,source_directory 中的所有內容(包括子目錄和文件)都會被遞歸復制到 destination_directory。可以在文件操作中保持目錄結構的完整性。
(2)遞歸移動
shutil.move(src, dst) 函數可以用于遞歸地移動文件和目錄,包括它們的子目錄和內容。可以在不同目錄之間移動文件和目錄,并且可以用于重命名文件或目錄。
使用 shutil.move 函數來遞歸地移動文件和目錄:
import shutil
# 源目錄或文件和目標目錄或文件
source = "source_path"
destination = "destination_path"
# 使用 move 函數遞歸移動源到目標
shutil.move(source, destination)
print(f"'{source}' has been recursively moved to '{destination}'.")
source 可以是文件或目錄,它及其內容將被遞歸地移動到 destination。如果 destination 是目錄,那么 source 將成為 destination 目錄的子目錄。如果 destination 是文件路徑,那么 source將被移動并重命名為 destination。
(3)遞歸刪除
shutil.rmtree(directory) 函數用于遞歸刪除目錄及其內容,包括子目錄和文件。可以輕松地清理整個目錄樹。
使用 shutil.rmtree 函數來遞歸刪除目錄:
import shutil
# 要刪除的目錄
directory_to_delete = "directory_to_delete"
# 使用 rmtree 函數遞歸刪除目錄及其內容
shutil.rmtree(directory_to_delete)
print(f"Directory '{directory_to_delete}' has been recursively deleted.")
在上面的示例中,shutil.rmtree 函數會刪除 directory_to_delete 目錄以及其中的所有子目錄和文件。這是一個非常有用的功能,特別需要清理或卸載不再需要的目錄時。
4、示例應用:備份文件
讓我們看一個實際示例,使用 shutil 模塊創建一個簡單的文件備份腳本。
import shutil
import os
import time
# 源目錄和目標目錄
source_directory = "source_data"
backup_directory = "backup_data"
# 創建目標目錄(如果不存在)
if not os.path.exists(backup_directory):
os.makedirs(backup_directory)
# 獲取當前日期作為備份文件夾名稱
backup_folder = time.strftime("%Y-%m-%d")
# 創建以當前日期為名稱的備份子目錄
backup_path = os.path.join(backup_directory, backup_folder)
os.makedirs(backup_path)
# 復制源目錄中的內容到備份目錄
shutil.copytree(source_directory, os.path.join(backup_path, source_directory))
此示例會創建一個備份文件夾,其中包含了源目錄中的內容,以當前日期作為子目錄名稱。
5、結語
shutil 模塊是 Python 編程中的一個強大工具,用于進行文件和目錄操作。不僅可以進行基本的文件復制、移動、重命名和刪除,還可以遞歸地處理目錄結構。通過深入了解 shutil 模塊的功能,可以更好地掌握如何使用它來處理文件和目錄,從而提高代碼的效率和可維護性。