【Python 檔案處理 #2】利用 shutil 來複製檔案 shutil copy file(內附範例程式碼)

前言

shutil 是 python 的高階檔案操作 package

範例程式碼 - 複製「檔案」

import shutil

src = "./input_path/origin_file.txt"
dst = "./output_path/copy_file.txt"
shutil.copyfile(src, dst)

然後就會把 src 的檔案複製到 dst 囉!

範例程式碼 - function 複製「檔案」進「資料夾」

def put_file_in_folder(folder, file):
    if not os.path.isdir(folder):
        os.makedirs(folder, mode=0o777) # absolute makedirs

    print(f"put {file} in to the folder {folder}")
    shutil.copy(file, folder)

Reference