可以使用shutil模塊中的函數(shù)來(lái)復(fù)制目錄下的所有文件到另外一個(gè)目錄。
import shutil
import os
def copy_files(src_dir, dst_dir):
# 遍歷源目錄下的所有文件和文件夾
for item in os.listdir(src_dir):
src_item = os.path.join(src_dir, item) # 源文件路徑
dst_item = os.path.join(dst_dir, item) # 目標(biāo)文件路徑
if os.path.isfile(src_item): # 如果是文件,則直接復(fù)制
shutil.copy2(src_item, dst_item)
elif os.path.isdir(src_item): # 如果是文件夾,則遞歸調(diào)用復(fù)制函數(shù)
shutil.copytree(src_item, dst_item)
在調(diào)用`copy_files`函數(shù)時(shí),需要傳入源目錄和目標(biāo)目錄的路徑。例如:文章源自網(wǎng)吧系統(tǒng)維護(hù)-http://www.hvig.cn/11072.html
src_dir = '/path/to/source/directory' dst_dir = '/path/to/destination/directory' copy_files(src_dir, dst_dir)
這樣就能將`src_dir`目錄下的所有文件和文件夾復(fù)制到`dst_dir`目錄中。注意,如果目標(biāo)目錄不存在,會(huì)自動(dòng)創(chuàng)建該目錄。文章源自網(wǎng)吧系統(tǒng)維護(hù)-http://www.hvig.cn/11072.html 文章源自網(wǎng)吧系統(tǒng)維護(hù)-http://www.hvig.cn/11072.html
版權(quán)聲明:文章圖片資源來(lái)源于網(wǎng)絡(luò),如有侵權(quán),請(qǐng)留言刪除!!!


評(píng)論