从input.txt中逐行读取写为list,随机选择N个写入output.txt
import os
import numpy as np
import random
os.chdir(os.path.abspath(os.path.dirname(os.path.abspath(__file__)) + os.path.sep + "."))
N = 10
in_path = "./input.txt"
cur_path = os.getcwd()
out_path = os.path.join(cur_path, "output.txt")
lists = list()
f = open(in_path, "r")
lines = f.readlines()
for line in lines:
line = line.strip("\n")
lists.append(line)
f.close()
sele_lists = random.sample(lists, N)
with open(out_path, 'a+') as f:
for file in sele_lists:
f.writelines(file + "\n")
f.close()
python中从一个文件夹中随机选择N个样本复制
import os
import shutil
import numpy as np
src_path = './src'
dst_path = './dst'
imgs = os.listdir(src_path)
sele_imgs = np.random.choice(imgs, 10000, replace=False)
lists = [os.path.join(src_path, x) for x in sele_imgs]
for img in lists:
shutil.copy(img, dst_path)