코드민수
[Python] 폴더 내 파일 일부 랜덤하게 복사 본문
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
'[Python] > 코드' 카테고리의 다른 글
[Tensorboard] 로그파일 열기 (0) | 2023.06.14 |
---|---|
[Python] plt.savefig() 시 생기는 여백 제거 방법 (0) | 2023.05.10 |
[Python] 경로 내 이미지 전체의 평균, 표준편차 구하기 (0) | 2023.05.06 |
[Python] Tensorflow & Pytorch GPU 사용 확인 (0) | 2023.04.07 |
[Python] 레이블 없는 이미지 찾기 (0) | 2023.04.03 |