如何使用 Python 將特定檔案從一個資料夾複製到另一個資料夾?
在廣闊的計算機程式設計領域,高效管理檔案和目錄的技能至關重要。Python 作為一種用途廣泛且功能強大的程式語言,提供了豐富的工具來有效地操作檔案。一個經常出現的常見任務是需要將特定檔案從一個資料夾複製到另一個資料夾。在這篇啟迪性的文章中,我們將深入瞭解使用 Python 實現此過程的方法。我們將逐步探索各種技術,並提供一些實用的程式碼示例。
理解手頭的任務
在我們開始複製檔案之前,讓我們花一點時間來理解任務的本質。目標是掃描原始檔夾,根據某些條件識別特定檔案,然後將這些選定的檔案複製到目標資料夾。我們可以利用 Python 的標準庫模組(如 os、shutil 和 glob)來實現這一點。
複製所有檔案
讓我們從一個基本示例開始,我們將原始檔夾中的所有檔案複製到目標資料夾。我們將使用 os 模組遍歷原始檔夾中的檔案,並使用 shutil 模組執行實際的複製操作。
示例
在這個示例中,我們定義了一個名為 copy_all_files 的函式,它將原始檔夾和目標資料夾的路徑作為引數。我們使用 os.listdir() 獲取原始檔夾中所有檔案的列表,然後遍歷它們。對於每個檔案,我們使用 os.path.join() 構造其完整路徑。os.path.isfile() 函式檢查該專案是否為檔案(而不是目錄)。如果它是一個檔案,我們使用 shutil.copy() 將其複製到目標資料夾。
import os import shutil def copy_all_files(source_folder, target_folder): for filename in os.listdir(source_folder): source_file = os.path.join(source_folder, filename) if os.path.isfile(source_file): shutil.copy(source_file, target_folder) # Example usage copy_all_files('source_folder', 'target_folder')
複製具有特定副檔名的檔案
有時,我們可能只想複製具有某些副檔名的檔案。例如,我們可能只想複製 .txt 檔案或 .jpg 圖片。讓我們修改之前的示例來實現這一點。
示例
在這個修改後的版本中,我們在函式中添加了一個額外的副檔名引數。然後,我們在將檔案複製到目標資料夾之前檢查每個檔名的結尾是否與指定的副檔名匹配。
import os import shutil def copy_files_with_extension(source_folder, target_folder, extension): for filename in os.listdir(source_folder): source_file = os.path.join(source_folder, filename) if os.path.isfile(source_file) and filename.endswith(extension): shutil.copy(source_file, target_folder) # Example usage copy_files_with_extension('source_folder', 'target_folder', '.txt')
基於檔名模式複製檔案
如果我們想根據檔名中的特定模式複製檔案怎麼辦?例如,我們可能希望複製所有以“report_”開頭或以“_final”結尾的檔案。我們可以使用 Python 中的 glob 模組來實現這一點。
示例
在這個示例中,我們定義了一個名為 copy_files_with_pattern 的函式,它將原始檔夾、目標資料夾和所需的模式作為引數。我們使用 glob.glob() 獲取原始檔夾中與指定模式匹配的檔案路徑列表。
import shutil import glob def copy_files_with_pattern(source_folder, target_folder, pattern): for file_path in glob.glob(os.path.join(source_folder, pattern)): if os.path.isfile(file_path): shutil.copy(file_path, target_folder) # Example usage copy_files_with_pattern('source_folder', 'target_folder', 'report_*')
複製在日期範圍內修改的檔案
有時,我們需要複製在特定日期範圍內修改的檔案。例如,我們可能希望複製過去 7 天內修改的所有檔案。讓我們探討一下如何實現這一點。
示例
在這個示例中,我們定義了一個名為 copy_files_within_date_range 的函式,它將原始檔夾、目標資料夾和天數作為引數。我們使用 time.time() 從當前時間減去天數(以秒為單位)來計算截止時間。然後,我們在將檔案複製到目標資料夾之前檢查每個檔案的修改時間(os.path.getmtime())是否大於或等於截止時間。
import shutil import os import time def copy_files_within_date_range(source_folder, target_folder, days): cutoff_time = time.time() - days * 86400 for filename in os.listdir(source_folder): source_file = os.path.join(source_folder, filename) if os.path.isfile(source_file) and os.path.getmtime(source_file) >= cutoff_time: shutil.copy(source_file, target_folder) # Example usage to copy files modified within the last 7 days copy_files_within_date_range('source_folder', 'target_folder', 7)
基於檔案大小複製檔案
最後,讓我們探討一下如何根據檔案大小複製檔案。我們可能希望複製所有大於特定大小的檔案,或複製在特定大小範圍內的檔案。以下是如何實現它。
示例
在這個最後一個示例中,我們定義了一個名為 copy_files_within_size_range 的函式,它將原始檔夾、目標資料夾、最小大小和最大大小作為引數。我們使用 os.path.getsize() 獲取每個檔案的大小(以位元組為單位),然後在將檔案複製到目標資料夾之前檢查該大小是否在指定的範圍內。
import shutil import os def copy_files_within_size_range(source_folder, target_folder, min_size, max_size): for filename in os.listdir(source_folder): source_file = os.path.join(source_folder, filename) if os.path.isfile(source_file): file_size = os.path.getsize(source_file) if min_size <= file_size <= max_size: shutil.copy(source_file, target_folder) # Example usage to copy files between 1 MB and 10 MB copy_files_within_size_range('source_folder', 'target_folder', 1000000, 10000000)
在這篇詳盡的文章中,我們探討了使用 Python 將特定檔案從一個資料夾複製到另一個資料夾的不同方法。我們從一個複製所有檔案的簡單示例開始,然後逐步過渡到更專業的案例,其中我們根據副檔名、檔名模式、修改日期和檔案大小複製檔案。Python 的標準庫模組(如 os、shutil 和 glob)被證明是完成這些任務的強大工具。掌握了這些知識,您現在就可以自信地輕鬆地在 Python 專案中管理檔案複製操作。所以,請繼續前進,利用 Python 的強大功能,掌握檔案操作的技巧!