Pin-Jiun / Python

Python Document
0 stars 0 forks source link

shutil #41

Open Pin-Jiun opened 8 months ago

Pin-Jiun commented 8 months ago

你可以使用 shutil 模块的 copy 函数来复制文件。以下是一个简单的示例:

python Copy code import shutil

source_path = '原始文件的路径' destination_path = '目标文件夹的路径'

shutil.copy(source_path, destination_path) 在上述示例中,将原始文件复制到目标文件夹。确保替换 '原始文件的路径' 和 '目标文件夹的路径' 为你实际的文件路径和目标路径。

如果你想要保留相同的文件名,只是更改目标文件夹,你可以使用 os.path.join 来构建目标路径:

python Copy code import shutil import os

source_path = '原始文件的路径' destination_folder = '目标文件夹的路径'

获取原始文件名

file_name = os.path.basename(source_path)

构建目标路径

destination_path = os.path.join(destination_folder, file_name)

复制文件

shutil.copy(source_path, destination_path) 这样,你将保留相同的文件名,只是复制到了新的目标文件夹。

Pin-Jiun commented 8 months ago

import shutil

for img in data:
    label = img["label"]

    source_path = f"..{f_p}"

    destination_path = f"../ta_dataset/{label}/"

    shutil.copy(source_path, destination_path)