在Python中,你可以使用shutil
模塊來進行文件復制。以下是一個簡單的示例:
import shutil
def copy_file(src, dst):
shutil.copy2(src, dst)
source_file = 'path/to/source/file.txt'
destination_file = 'path/to/destination/file.txt'
copy_file(source_file, destination_file)
在這個示例中,我們首先導入shutil
模塊,然后定義一個名為copy_file
的函數,該函數接受兩個參數:源文件路徑(src
)和目標文件路徑(dst
)。我們使用shutil.copy2()
函數來復制文件,它會保留文件的元數據(如創建時間、修改時間等)。最后,我們指定源文件和目標文件的路徑,并調用copy_file
函數來執行復制操作。