Recent Posts
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
Today
Total
관리 메뉴

코드민수

[Python] 폴더 내 파일 일부 랜덤하게 복사 본문

[Python]/코드

[Python] 폴더 내 파일 일부 랜덤하게 복사

코드민수 2023. 7. 6. 13:20
BIG

경로 A에서 경로 B로 n 개의 파일을 복사하는 파이썬 코드입니다

import os
import random
import shutil
from tqdm import tqdm

def copy_random_files(source_path, destination_path, num_files):
    files = os.listdir(source_path)
    # 파일 목록에서 랜덤하게 num_files 개의 파일 선택
    random_files = random.sample(files, num_files)

    # 파일을 경로 B로 복사
    for file in tqdm(random_files):
        source_file = os.path.join(source_path, file)
        destination_file = os.path.join(destination_path, file)
        shutil.copy2(source_file, destination_file)

source_path = "경로 A"
destination_path = "경로 B"
num_files = 2000

copy_random_files(source_path, destination_path, num_files)

source_path: 복사할 파일이 있는 폴더

destination_path: 파일을 복사할 폴더

num_files: 복사할 파일의 수

 

경로 A의 파일 중 랜덤하게 num_files 개의 파일이 경로 B에 복사됩니다

LIST